comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » Specify the degree of accuracy of a floating point number
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Switch to threaded view of this topic Create a new topic Submit Reply
Specify the degree of accuracy of a floating point number [message #52607] Wed, 21 February 2007 20:48 Go to next message
loknath is currently offline  loknath
Messages: 11
Registered: May 2006
Junior Member
Hi
Is there a way to convert a single precision or double precision
floating point number to an accuracy of specified decimal places? In
other words, if I want to write 0.268954 or 233.256 to an accuracy of
2 decimal places, i.e. 0.27 and 233.26, is there a way to do it?
Thanks
Re: Specify the degree of accuracy of a floating point number [message #52726 is a reply to message #52607] Sat, 24 February 2007 07:12 Go to previous message
David Fanning is currently offline  David Fanning
Messages: 11724
Registered: August 2001
Senior Member
Kenneth Bowman writes:

> This is a round-off error issue. The function works by multiplying the
> input by a factor, rounding to a LONG, and then dividing by the same factor
> to convert back to a floating-point type.
>
> When you are trying to preserve a lot of precision (digits), you get round-off
> issues with the LONG. This can be avoided (in most cases), by rounding to
> a LONG64.

Ah, thanks for this. I was using the L64 keyword for ROUND
in NUMBER_FORMATTER, but I was doing some of the other math
with FLOAT and LONG. By changing these to DOUBLE and LONG64
I've managed to make NUMBER_FORMATTER work properly with
all the perverse cases I've tried.

I've put an updated version here:

http://www.dfanning.com/programs/number_formatter.pro

Cheers,

David

--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.dfanning.com/
Sepore ma de ni thui. ("Perhaps thou speakest truth.")
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Re: TRIANGULATE. Finding contiguous cells efficiently?
Next Topic: Fast way to calculate sum of discrete poisson distribution

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Sat Oct 11 03:33:12 PDT 2025

Total time taken to generate the page: 0.08257 seconds