I know that Math.sqrt calls StrictMath.sqrt(double a) I was wanting to look at the actual code used to calculate it.
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
When you install a JDK the source code of the standard library can be found inside
src.zip
. This won't help you forStrictMath
, though, asStrictMath.sqrt(double)
is implemented as follows:So it's really just a native call and might be implemented differently on different platforms by Java.
However, as the documentation of
StrictMath
states:So by finding the appropriate version of the
fdlibm
source, you should also find the exact implementation used by Java (and mandated by the specification here).The implementation used by
fdlibm
isI don't know exactly but I think you'll find Newton's algorithm at the end point.
UPD: as comments say concrete implementation depends on concrete java machine. For windows it's probably using assembler implementation, where the standard operator sqrt exists
Since I happen to have OpenJDK lying around, I'll show its implementation here.
In jdk/src/share/native/java/lang/StrictMath.c:
jsqrt
is defined assqrt
in jdk/src/share/native/java/lang/fdlibm/src/w_sqrt.c (the name is changed through the preprocessor):And
__ieee754_sqrt
is defined in jdk/src/share/native/java/lang/fdlibm/src/e_sqrt.c as:There are copious comments in the file explaining the methods used, which I have omitted for (semi-) brevity. Here's the file in Mercurial (I hope this is the right way to link to it).
Download the sourcecode from the OpenJDK.