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

Home » Public Forums » archive » Re: Numbers from nowhere?
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
Re: Numbers from nowhere? [message #58725] Sun, 17 February 2008 13:53 Go to next message
David Fanning is currently offline  David Fanning
Messages: 11724
Registered: August 2001
Senior Member
elwood writes:

> But my question is more pointed:
> if you assign x=3.3
> and you know apriori that the floating point data type
> will not have enough bits to store this number precisely,
> why does "print" show this number as 3.3?
>
> Is it because it is rounding off with some kind of algorithm?
> Or am I misinterpreting how this number is being stored?

I presume it is because whatever number *is* stored,
when rounded to the 7-8 significant figures a float
can accurately represent, comes out to 3.300000.

IDL> x=3.3
IDL> print, x, format='(f0)'
3.300000

IDL> print, x, format='(f12.10)'
3.2999999523

Cheers,

David
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming (www.dfanning.com)
Sepore ma de ni thui. ("Perhaps thou speakest truth.")
Re: Numbers from nowhere? [message #58726 is a reply to message #58725] Sun, 17 February 2008 12:19 Go to previous messageGo to next message
elwood is currently offline  elwood
Messages: 23
Registered: February 2007
Junior Member
I'm glad that the Coyotes have something useful to do, besides keeping
me awake
all night in the Anza Borrego desert (it was wonderful to hear them
all night actually!)

But my question is more pointed:
if you assign x=3.3
and you know apriori that the floating point data type
will not have enough bits to store this number precisely,
why does "print" show this number as 3.3?

Is it because it is rounding off with some kind of algorithm?
Or am I misinterpreting how this number is being stored?

Curious,
Elisha

On Feb 15, 4:51 pm, David Fanning <n...@dfanning.com> wrote:
> Paul van Delst writes:
>> Cosmic rays randomly flipping the bits beyond the stated precision?
>
> Teams of coyotes, given jobs like this to satisfy
> their playful nature, but still keep them off the
> street, more likely.
>
> Cheers,
>
> David
> --
> David Fanning, Ph.D.
> Fanning Software Consulting, Inc.
> Coyote's Guide to IDL Programming (www.dfanning.com)
> Sepore ma de ni thui. ("Perhaps thou speakest truth.")
Re: Numbers from nowhere? [message #58736 is a reply to message #58726] Fri, 15 February 2008 14:51 Go to previous messageGo to next message
David Fanning is currently offline  David Fanning
Messages: 11724
Registered: August 2001
Senior Member
Paul van Delst writes:

> Cosmic rays randomly flipping the bits beyond the stated precision?

Teams of coyotes, given jobs like this to satisfy
their playful nature, but still keep them off the
street, more likely.

Cheers,

David
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming (www.dfanning.com)
Sepore ma de ni thui. ("Perhaps thou speakest truth.")
Re: Numbers from nowhere? [message #58737 is a reply to message #58736] Fri, 15 February 2008 13:31 Go to previous messageGo to next message
Paul Van Delst[1] is currently offline  Paul Van Delst[1]
Messages: 1157
Registered: April 2002
Senior Member
Conor wrote:
> I'm just curious:
>
> print,3.3,format='(f30.28)'
> 3.2999999523162841796875000000
>
> Obviously float is only good to ~6 decimal places, but IDL will still
> print out additional digits. It seems that it does reach a point
> where IDL will only print out zeroes. I'm just curious - where do
> these extra digits come from? Does it come from rounding errors
> converting binary to decimal? Or does it come from some other
> nefarious location?

Cosmic rays randomly flipping the bits beyond the stated precision?

:o)
Re: Numbers from nowhere? [message #58738 is a reply to message #58737] Fri, 15 February 2008 13:12 Go to previous messageGo to next message
bennette is currently offline  bennette
Messages: 1
Registered: February 2008
Junior Member
On Feb 15, 10:08 am, Conor <cmanc...@gmail.com> wrote:
> I'm just curious:
>
> print,3.3,format='(f30.28)'
> 3.2999999523162841796875000000
>
> Obviously float is only good to ~6 decimal places, but IDL will still
> print out additional digits. It seems that it does reach a point
> where IDL will only print out zeroes. I'm just curious - where do
> these extra digits come from? Does it come from rounding errors
> converting binary to decimal? Or does it come from some other
> nefarious location?

I think that you hit the nail on the head. There is a nice article on
decimal to binary conversion on David Fanning's sight.
http://www.dfanning.com/math_tips/sky_is_falling.html

Eric

----------------
Eric Bennett
National Institutes of Health
bennette at nih.nhlbi.gov
Re: Numbers from nowhere? [message #58780 is a reply to message #58725] Thu, 21 February 2008 05:06 Go to previous messageGo to next message
Sven Utcke is currently offline  Sven Utcke
Messages: 10
Registered: October 2007
Junior Member
David Fanning <news@dfanning.com> writes:

> elwood writes:
>
>> But my question is more pointed: if you assign x=3.3 and you know
>> apriori that the floating point data type will not have enough bits
>> to store this number precisely, why does "print" show this number
>> as 3.3?
>
> I presume it is because whatever number *is* stored, when rounded to
> the 7-8 significant figures a float can accurately represent, comes
> out to 3.300000.

What number _is_ stored, actually? Assuming we are talking ieee, we
have one bit for the sign, 8 for the exponent, and 23 for the
mantissa. So what is 3.3?

3 = 11 = 1.1 * 2^1
0.3 = 0.0100110011001100110011001100110011001100...
which we see from

0.3*2 = _0_.6
0.6*2 = _1_.2
0.2*2 = _0_.4
0.4*2 = _0_.8
0.8*2 = _1_.6
0.6*2 = ...

so we get, combined,

3.3 = 1.10100110011001100110011 * 2^1

or

S | Exp + 127 | Mantissa without leading 1
0 | 1000000 | 1010011 00110011 00110011

which, if we recombine it, turns out to be 3.2999999523162841796875

We can actually see this in IDL too:

IDL> print, byte(3.3,0,4)
51 51 83 64
Which, if we rewrite it appropriately, turns out to be:

01000000 01010011 00110011 00110011

which, recombined differently, is the above number :-)

Sven
--
___ _ _____ ___ Dr.-Ing. Sven Utcke ___ ___ _____ __
/ __| |/ / __| __| phone: +49 40 8998-5317 | \| __/ __\ \ / /
| (_ | ' <\__ \__ \ fax : +49 40 8994-5317 (NEW) | |) | _|\__ \\ V /
\___|_|\_\___|___/ http://www.desy.de/~utcke (to come)|___/|___|___/ |_|
Re: Numbers from nowhere? [message #58920 is a reply to message #58780] Mon, 25 February 2008 09:57 Go to previous message
elwood is currently offline  elwood
Messages: 23
Registered: February 2007
Junior Member
Thanks,
very succinct and clear explanation.
Fun with Binary and Computer Precision is what I summarize
the topic as ;-)

-Elisha

On Feb 21, 7:06 am, Sven Utcke <utcke+n...@informatik.uni-hamburg.de>
wrote:
> David Fanning <n...@dfanning.com> writes:
>> elwood writes:
>
>>> But my question is more pointed: if you assign x=3.3 and you know
>>> apriori that the floating point data type will not have enough bits
>>> to store this number precisely, why does "print" show this number
>>> as 3.3?
>
>> I presume it is because whatever number *is* stored, when rounded to
>> the 7-8 significant figures a float can accurately represent, comes
>> out to 3.300000.
>
> What number _is_ stored, actually? Assuming we are talking ieee, we
> have one bit for the sign, 8 for the exponent, and 23 for the
> mantissa. So what is 3.3?
>
> 3 = 11 = 1.1 * 2^1
> 0.3 = 0.0100110011001100110011001100110011001100...
> which we see from
>
> 0.3*2 = _0_.6
> 0.6*2 = _1_.2
> 0.2*2 = _0_.4
> 0.4*2 = _0_.8
> 0.8*2 = _1_.6
> 0.6*2 = ...
>
> so we get, combined,
>
> 3.3 = 1.10100110011001100110011 * 2^1
>
> or
>
> S | Exp + 127 | Mantissa without leading 1
> 0 | 1000000 | 1010011 00110011 00110011
>
> which, if we recombine it, turns out to be 3.2999999523162841796875
>
> We can actually see this in IDL too:
>
> IDL> print, byte(3.3,0,4)
> 51 51 83 64
> Which, if we rewrite it appropriately, turns out to be:
>
> 01000000 01010011 00110011 T
> which, recombined differently, is the above number :-)
>
> Sven
> --
> ___ _ _____ ___ Dr.-Ing. Sven Utcke ___ ___ _____ __
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Labeling the x-axis with a non standard time string
Next Topic: idl format codes and output files

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

Current Time: Wed Oct 08 15:27:22 PDT 2025

Total time taken to generate the page: 0.00710 seconds