translating big numbers

Q&A's, tips, howto's
Locked
joejoe
Posts: 173
Joined: Thu Jun 25, 2009 5:09 pm
Location: Denver, USA

translating big numbers

Post by joejoe »

Hi,

I often get this sort of number:

-2.000000000000049e-07

I would like number to just display itself, without the "e-" exponent thing.

I don't mind zeros and I have a big screen and it would love to see the real number.

Would there be an easy way to convert the e number back into normal digits?

Thanks much! :-)

ralph.ronnquist
Posts: 228
Joined: Mon Jun 02, 2014 1:40 am
Location: Melbourne, Australia

Re: translating big numbers

Post by ralph.ronnquist »

There is the interesting issue of "precision" in this, i.e. how precises the decimal representation is realative to the actual numberical value held. For example, you might define the following functions:

Code: Select all

(define (digits n)
    (let (x 0 m 1)
        (while (!= n (add n m)) (inc x) (setf m (div m 10)))
        x))

(define (P n) (format (string "%0." (digits n) "f") n))
The first function , digits, determines how many fractional digits are representable, in terms of at which fractional position adding 1 at that position doesn't affect the represented number.

One might take that to be how many fractional digits to present, which is what the second function, P, implements. That function returns the number translated to a string (using the "f" format specifier) with a precision of that many fractional digits.

Anything more is kind of senseless, even though the normal "f" translation would happily offer some 76 non-zero fractional digits (which presumably arises due to the mechanics of mapping the number to a decimal representation). But if you want to look at zeroes, you could do

Code: Select all

(format "%0.1000f" -2.000000000000049e-07)
:)

joejoe
Posts: 173
Joined: Thu Jun 25, 2009 5:09 pm
Location: Denver, USA

Re: translating big numbers

Post by joejoe »

Ok super, thanks Ralph!

format always seals the deal. I see it now.

Out of curiousity, where are all the numbers coming from after the 0's in the following?

Code: Select all

> (format "%0.100f" -2.000000000000049e-07)
"-0.0000002000000000000048878588486791010869758622447989182546734809875488281250000000000000000000000000"
I would think it would be '"-0.00000020000000000000490000...'?

Locked