# Floating point trickery - Mac Programming

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...

1. ## Floating point trickery

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
JeffS87 Guest

leeg Guest

3. ## Re: Floating point trickery

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.
Michael Guest

4. ## Re: Floating point trickery

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?

thanks
Jeff
JeffS87 Guest

5. ## Re: Floating point trickery

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
<http://research.microsoft.com/~hollasch/cgindex/coding/ieeefloat.html>

hth

meeroh

--
If this message helped you, consider buying an item
from my wish list: <http://web.meeroh.org/wishlist>

Miro Guest

6. ## Re: Floating point trickery

Miro Jurisic wrote:

And from her down the left coast, there is

"What Every Computer Scientist Should Know About Floating-Point Arithmetic"
http://docs.sun.com/source/806-3568/ncg_goldberg.html
and/or
http://docs-pdf.sun.com/800-7895/800-7895.pdf

Good stuff!

Mike Guest

7. ## Re: Floating point trickery

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?"

Jeff

JeffS87 Guest

8. ## Re: Floating point trickery

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 num = 8388608.0;
num += 3.1415926;
num -= 8388608.0;

num has a value of 3.0

Jeff
JeffS87 Guest

9. ## Re: Floating point trickery

In Article aol.com, JeffS87 wrote:

Try it with 2.718281828 and you'll get 3.0.

--
Mike Kluev

Mike Guest

10. ## Re: Floating point trickery

In article <aol.com>, jeffs87
aol.com says...

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;
James Guest

11. ## Re: Floating point trickery

<< Try it with 2.718281828 and you'll get 3.0.

http://developer.apple.com/hardware/ve/algorithms.html#fast_floor

A Fast Floor routine that shows how to deal with that.

Jeff
JeffS87 Guest

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•