[racket] ~r: formatting 0.0 with 'exponential and #:precision

From: Dmitry Pavlov (dpavlov at ipa.nw.ru)
Date: Wed May 8 06:11:25 EDT 2013

Hi Jos,

I have nothing sensible to say about line 340 and
the fix you propose (probably it is right), but the
initial problem with 0.0 originates not in line 340
but rather in lines 330-331

(if (zero? N-abs)
     (values 0 0 (if exactly? significand-precision 0))

or line 311

[exponent (- significand-precision e-adjust)

When we do (~r 0.0 #:notation 'exponential #:precision 4),
significand-precision is equal to 4 and e-adjust happens
to be zero.


Regards,

Dmitry



On 05/08/2013 11:21 AM, Jos Koot wrote:
> I came across another thing:
>
> #lang racket
>
> (define x #e100.e-50000)
> (rational? x) ; #t
> (log x) ; -inf.0
> (exact->inexact x); 0.0
> (~r x #:notation 'exponential) ; error inexact->exact: no exact
> representation
>    number: -inf.0
>
> I think this is introduced in line 340 of racket/format.rkt which contains:
> (inexact->exact (floor (/ (log N-abs) (log base))))
> for the first estimate of the order of magnitude.
>
> As the docs of ~r say "The exactness or inexactness of x does not affect its
> formatting.", this behaviour may well be intentional.
>
> It is possible though to avoid the error by subtracting the log of the
> denominator from that of the nominator after conversion to exact rational.
> Proc order-of-magnitude (in math I think) does this, but is for base 10
> only. May be I should adapt order-of-magnitude such as to accept a base
> argument?
>
> Best wishes, Jos
>
>> -----Original Message-----
>> From: users-bounces at racket-lang.org
>> [mailto:users-bounces at racket-lang.org] On Behalf Of Dmitry Pavlov
>> Sent: martes, 07 de mayo de 2013 19:12
>> To: users at racket-lang.org
>> Subject: [racket] ~r: formatting 0.0 with 'exponential and #:precision
>>
>> Hello,
>>
>> I just came across the following oddness with formatting
>> of real numbers:
>>
>>   > (~r 1.0 #:notation 'exponential #:precision '(= 4))
>> "1.0000e+00"
>>
>> OK
>>
>>
>>   > (~r 0.0 #:notation 'exponential #:precision '(= 4))
>> "0.e+04"
>>
>> ???
>>
>> Is there a rational explanation of why did
>> the requested precision go into the exponent
>> number for 0.0?
>>
>> Thanks.
>>
>>
>> Best regards,
>>
>> Dmitry
>> ____________________
>>    Racket Users list:
>>    http://lists.racket-lang.org/users
>
>
>


Posted on the users mailing list.