DarkDragon,
I don't know Java but it is very likely to use the same format for it's floats as everywhere else. It would be silly not to as most hardware follows the same floating point standard.
Even if it doesn't, there's no need for a "conversion" to IEE 754. You just need to extract the bits in the mantissa and exponenet from wherever they happen to be.
http://www.javaworld.com/javaworld/jw-1 ... tml?page=2
And here
http://www.math.utah.edu/~beebe/software/ieee/ I found this quote:
The architectural specification of Java requires IEEE 754 arithmetic, but only a subset of it,
So it looks like the same IEEE754 spec is used by Java.
Helle,
That's the same basic algorithm I outlined above and you can speed it up in a number of ways.
By extracting the exponent as I mentioned earlier you save that While Z>1 loop with its slow divide each time. This can take a lot of time especially if the initial number is large and DarkDragon did say he wants it fast for large numbers.
As a half-way step, use Z=Z*0.5 instead of the slower Z=Z/2. For large initial Z you'll notice the time saving.
You can also rearrange the arithmetic inside the FOR N loop to reduce the calculations required each loop to less than half of what you're doing. You can get it down to 2 adds and 1 multiply for each iteration of the FOR loop.
You can even do away with the loop completely since 4 iterations gives sufficient accuracy you can just precalculate a lot of the values and assign them as constants.
The entire code, after the scaling, can be reduced to something like this:
Code: Select all
'define a few constants
onethird=0.33333333
onefifth=0.2
oneseventh=0.14285714
k=0.868588964 '2*LOG10(e) for base conversion
'the numer to get the LOG10 of (we assume it's been scaled to between 0.5 and 1.0)
z=0.7500033
'now the calculation
x=(z-1)/(z+1)
x2=x * x
intermediate= x * x2
answer= x + intermediate * onethird
intermediate= intermediate * x2
answer= answer + intermediate * onefifth
intermediate= intermediate * x2
answer= answer + intermediate * oneseventh
answer=answer * k 'convert to BASE10 and scale by 2 as needed by the formula
Notice that there is only 1 divide, 8 multiplies and 5 add/subtracts to get accuracy of about 5 decimal digits. That's not bad for a LOG10().
Paul.