admin管理员组文章数量:1302333
I am developing a translator that converts JavaScript source into a target language. I am trying to implement JavaScript's Math object in the target language.
If there is a JavaScript implementation of the "Math" object, I can use the translator to obtain 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 would help me avoid manually writing the Math object's code in the target language.
I am developing a translator that converts JavaScript source into a target language. I am trying to implement JavaScript's Math object in the target language.
If there is a JavaScript implementation of the "Math" object, I can use the translator to obtain 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 would help me avoid manually writing the Math object's code in the target language.
Share asked Mar 25, 2012 at 20:24 user1198335user1198335 631 silver badge4 bronze badges 1- I don't know of such implementation. – Madara's Ghost Commented Mar 25, 2012 at 20:26
3 Answers
Reset to default 4The V8 implementation of math.js might provide you with some guidance, but of course it's riddled with placeholders for native function calls. You would have to be able to replace things like %Math_floor(x)
with the appropriate standard library based function call in the target language.
http://code.google./p/v8/source/browse/branches/bleeding_edge/src/math.js?spec=svn10758&r=10758
This is from the official ecmascript-262 specification:
NOTE The behaviour of the functions acos, asin, atan, atan2, cos, exp, log, pow, sin, sqrt, and tan is not precisely specified here except to require specific results for certain argument values that represent boundary cases of interest. For other argument values, these functions are intended to pute approximations to the results of familiar mathematical functions, but some latitude is allowed in the choice of approximation algorithms. The general intent is that an implementer should be able to use the same mathematical library for ECMAScript on a given hardware platform that is available to C programmers on that platform.
Although the choice of algorithms is left to the implementation, it is remended (but not specified by this standard) that implementations use the approximation algorithms for IEEE 754 arithmetic contained in fdlibm, the freely distributable mathematical library from Sun Microsystems (http://wwwlib/fdlibm).
Objects you need are:
Math.exp(x) // Returns the value of Ex
Math.pow(x,y) // Returns the value of x to the power of y
Other than these there are more which will help you that you will be needed in java. Those are
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
本文标签: JavaScript code for Math objectStack Overflow
版权声明:本文标题:JavaScript code for Math object - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741711240a2393856.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论