JavaScript code for the Math object

I am developing a translator that converts JavaScript source code to the target language. I am trying to implement a Java Math object in the target language.

If there is a JavaScript implementation of the Math object, I can use a translator to get the equivalent code in the target language.

I am looking for something like this:

var Math = { pow: function(...) {...} exp: function(...) {...} /* other methods of Math */ } 

Is there such an implementation that is available? This will help me avoid manually writing the Math object code in the target language.

+6
source share
3 answers

The implementation of math.js V8 may provide you with some recommendations, but, of course, it is riddled with placeholders for your own function calls. You would have to replace things like %Math_floor(x) with the corresponding target function call based on the standard library in the target language.

http://code.google.com/p/v8/source/browse/branches/bleeding_edge/src/math.js?spec=svn10758&r=10758

+4
source

This is from the official ecmascript-262 specification :

NOTE The behavior of the acos, asin, atan, atan2, cos, exp, log, pow, sin, sqrt and tan functions is not exactly specified here, except to require specific results for certain values ​​of the arguments that are of interest. For other values ​​of the arguments, these functions are designed to calculate approximations to the results of familiar mathematical functions, but with a wide selection of approximation algorithms. The general intention is that the developer should be able to use the same mathematical library for ECMAScript on this hardware platform, available for C programmers on this platform.

Although the choice of algorithms is left to implement, it is recommended (but not specified by this standard) that implementations use approximation algorithms for the IEEE 754 arithmetic contained in fdlibm, a free math library from Sun Microsystems ( http://www.netlib.org/fdlibm )

+3
source

Necessary objects:

 Math.exp(x) // Returns the value of Ex Math.pow(x,y) // Returns the value of x to the power of y 

Other than that, there’s something else to help you that you need Java. it

 Math.PI // returns PI Math.random(); // returns a random number Math.max(0, 150, 30, 20, -8, -200); // returns 150 Math.min(0, 150, 30, 20, -8, -200); // returns -200 Math.round(4.7); // returns 5 Math.round(4.4); // returns 4 
+2
source

Source: https://habr.com/ru/post/911616/


All Articles