• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: rounding a float
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: rounding a float


  • Subject: Re: rounding a float
  • From: Ed Watkeys <email@hidden>
  • Date: Sun, 30 Nov 2003 10:50:37 -0500

On Nov 30, 2003, at 12:16 AM, Jay Rimalrick wrote:

Can you explain why my last example where I used an int to get rid of the float
imperfections did not work?

Just to drive you crazy, here's the output from your program on my 1.25GHz Aluminum PowerBook:

2003-11-30 10:19:12.119 Transmogrify Studio[979] 0.620000
2003-11-30 10:19:16.472 Transmogrify Studio[979] 0.620000

That said, in the debugger, aFloat had the value "0.620000005".

Your algorithm would work fine, except that floating point math is inexact. You can't subtract the integer from a float unless you promote it to a float. That conversion can introduce error. Here's a program to blow your mind:

#include <stdlib.h>
#include <stdio.h>

int main(int argc, char **argv) {
float f = 1.0E6;
float g = 1.0E-2;
long i;

for(i=0; i < 100000000; i++)
f -= g;
printf("%f\n", f);

return 0;
}

The result should be zero, but it prints "1000000" on my computer. Things get much better if you switch the floats to doubles: the modified program prints "-0.000779", very close to the correct answer. The running time of the two programs were essentially identical: just under two seconds. (FOR ONE HUNDRED MILLION CALCULATIONS!) Don't scrimp on floating point precision thinking it's going to improve the runtime of your application. It will save you memory, but I only mention that because I'm an Apple II guy, and saving a few bytes used to mean the difference between your program fitting and not.

If you truly need arbitrarily precision floating point arithmetic, check out the GMP project at the following URL:

http://www.swox.com/gmp/

The library is LGPLed: it won't infect your programs, but you'll need to suffer through the documentation's self-righteousness.

Regards,
Ed

P.S. You may have noticed that I write all of my example applications as ANSI C: they're a lot easier to run that way, and you can ask C people in addition to Objective-C/Cocoa people -- who are far more numerous -- for advice.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.
References: 
 >Re: rounding a float (From: "Jay Rimalrick" <email@hidden>)

  • Prev by Date: Re: Getting NSTextField from NSWindow firstResponder
  • Next by Date: Dynamic UI
  • Previous by thread: Re: rounding a float
  • Next by thread: Re: rounding a float
  • Index(es):
    • Date
    • Thread