Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!usc!zaphod.mps.ohio-state.edu!sol.ctr.columbia.edu!lll-winken!unixhub!shelby!csli!francis From: francis@csli.Stanford.EDU (Dave Francis) Newsgroups: comp.sys.mac.programmer Subject: Floating Point accuracy in Think C Message-ID: <16594@csli.Stanford.EDU> Date: 28 Nov 90 20:34:05 GMT Reply-To: francis@csli.stanford.edu (Dave Francis) Organization: Center for the Study of Language and Information, Stanford U. Lines: 28 Hello netters! I have a roundoff problem when using floating point numbers in Think C 4.0.2. I've written a function which takes a floating point number and returns a string that can be used for displaying with QuickDraw. The problem is that when I pass the value to be converted Think C changes it to a less accurate number so when I generate the string I don't get exactly what was passed in. Here's a example: The number input is 2.314 but inside the routine it becomes 2.3139996765 or something like that (I think becuase Think C passes floating point as extended). Anyway when I generate the string the return is "2.313" if the precision is set to 3 (the function also takes a precision parameter) It gets worse if I pass a value which has less significant digits than the precision. So the value 3.2 passed in with a precision parameter of 3 in some cases gets returned as "3.199", which is unexceptable for display purposes if the user e ntered 3.2. Is there a way to stop this loss of accuracy when passing floating point values? Why is there a loss of accuracy anyway? What should I do? Are there any rounding functions out there or better float->string converters? Help!! Dave Francis Sapphire Design Systems