Professional Web Applications Themes

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

    Default 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

  2. Moderated Post

    Default Re: Floating point trickery

    Removed by Administrator
    leeg Guest
    Moderated Post

  3. #3

    Default 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. #4

    Default 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. #5

    Default 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. #6

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

    Default 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. #8

    Default 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, 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

    Jeff
    JeffS87 Guest

  9. #9

    Default Re: Floating point trickery

    In Article aol.com, JeffS87 wrote:
     

    Try it with 2.718281828 and you'll get 3.0.

    --
    Mike Kluev

    PS. Remove "-DELETE-." part of my e-mail address to reply.

    Mike Guest

  10. #10

    Default 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. #11

    Default 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

Similar Threads

  1. Floating point rounding issue
    By Brian in forum Informix
    Replies: 3
    Last Post: January 12th, 07:49 PM
  2. calculator, digits behind floating point
    By SchneiderPapier webforumsuser@macromedia.com in forum Macromedia Director Lingo
    Replies: 3
    Last Post: November 3rd, 08:20 AM
  3. Floating Point precision madness
    By rh2600 webforumsuser@macromedia.com in forum Macromedia Director Lingo
    Replies: 4
    Last Post: July 22nd, 09:49 AM
  4. From floating point to fraction
    By jon rogers in forum PERL Miscellaneous
    Replies: 6
    Last Post: July 5th, 08:19 PM

Bookmarks

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139