Java Reference
In-Depth Information
// Area of triangle in double
ad = 12345679.0 ;
bd = 12345678.0 ;
cd = 1.01233995 ;
sd = ( ad + bd + cd )/ 2.0d ;
aread = Math . sqrt ( sd * ( sd - ad ) * ( sd - bd ) * ( sd - cd ));
System . out . println ( "Double precision: " + aread );
}
}
Now let's run it. To ensure that the rounding is not an implementation artifact, I'll try it both
with Sun's JDK and with an ancient implementation of Kaffe (once an alternative Java SE
implementation):
$ java numbers.Heron
Single precision: 0.0
Double precision: 972730.0557076167
$ kaffe Heron
Single precision: 0.0
Double precision: 972730.05570761673
If in doubt, use double !
To ensure consistency of very large magnitude double computations on different Java imple-
mentations, Java provides the keyword strictfp , which can apply to classes, interfaces, or
methods within a class. [ 23 ] If a computation is Strict-FP, then it must always, for example, re-
turn the value INFINITY if a calculation would overflow the value of Double.MAX_VALUE (or
underflow the value Double.MIN_VALUE ). Non-Strict-FP calculations—the default—are al-
lowed to perform calculations on a greater range and can return a valid final result that is in
range even if the interim product was out of range. This is pretty esoteric and affects only
computations that approach the bounds of what fits into a double.
Comparing Floating-Point Numbers
Problem
You want to compare two floating-point numbers for equality.
 
Search WWH ::




Custom Search