Removed by Administrator
Hi, Is there a way to zero everything to the left of the decimal point in a floating point number? I know you can move it to an integer and then subtract the integer from the floating point number, but moving from int to float and float to int is slow. thanks Jeff...
Is there a way to zero everything to the left of the decimal point in a
floating point number? I know you can move it to an integer and then subtract
the integer from the floating point number, but moving from int to float and
float to int is slow.
Removed by Administrator
On Wed, 16 Jun 2004, JeffS87 wrote:
You could do myFloat - floorf(myFloat) or whichever function is
appropriate, and this will be faster. Check out the contents of math.h,
there may be a function that does exactly what you're looking for.
I tried floorl (the fastest of the 3) and it's about as fast as if you moved it
to an integer and subtracted it from the floating point number.
I tried fctiwz, but the upper 32 bits has stuff in it and it shows a value of
-nan. Is there an assembler instruction to clear the upper 32 bits of a
floating point register?
In article <aol.com>,
com (JeffS87) wrote:
I don't think you understand how floating point works if you think you can just
clear some bits to do this... I suggest reading
If this message helped you, consider buying an item
from my wish list: <http://web.meeroh.org/wishlist>
Miro Jurisic wrote:
And from her down the left coast, there is
"What Every Computer Scientist Should Know About Floating-Point Arithmetic"
I'm aware a floating point number is made up of an exponent and a mantissa. I
should have said "How do I make the number returned by fctiwz without having to
move it to memory?"
I did some poking around in Google groups and found "magic number". A
floating point number's mantissa is 23 bits, so if you first put 2^23 in the
float, adding a mixed number will only add the decimal part.
float num = 8388608.0;
num += 3.1415926;
num -= 8388608.0;
num has a value of 3.0
In Article aol.com, JeffS87 wrote:
Try it with 2.718281828 and you'll get 3.0.
PS. Remove "-DELETE-." part of my e-mail address to reply.
In article <aol.com>, jeffs87
If you look in the source for libm - specifically
ppc.subproj/ceilfloor.c you'll see that OS X uses exactly this trickery
in the various floor and ceiling functions. It also uses the magic
number 2^52 for doubles.
static const double twoTo52 = 4503599627370496.0;
static const float twoTo23 = 8388608.0;
<< Try it with 2.718281828 and you'll get 3.0.
A Fast Floor routine that shows how to deal with that.