Mailing List Archive: 49091 messages

## [REBOL] Re: strange results with decimal!

### From: joel:neely:fedex at: 14-Jan-2002 9:37

```
Hi, Thorsten,

Thorsten Moeller wrote:
> i learned from your responses that the result of 1E-2 from a
> substraction of 1,99 and 1,98
>   >> 1,99 - 1,98
>   == 1E-2
> comes from the binary handling of this decimals an is the same
> as 0,01 and will behave so in further operations. OK.
>
> Is it possible to display the value of 0,01 correctly, without
> checking each result and providing a corresponding string like
>
>   >> if c = 0,01 [print "0,01"]
>
> ?? (same for values lower than 0,01 like 0,001)
>

>From the point of view of scientific notation, 1E-2 and 0.01
*ARE* both correct representations of the same value.  If what
you're saying is that you want a "commercial" presentation of
that value, you can certainly do something resembling this

as-decimal-string: func [x [decimal!] /local r t] [
r: 0.01 * to-integer 100 * x + 0.5
t: join mold to-integer r "."
r: r - to-integer r
loop 2 [
r: r * 10.0
append t mold to-integer r
r: r - to-integer r
]
t
]

so that

>> 1.99 - 1.98
== 1E-2
>> as-decimal-string 1.99 - 1.98
== "0.01"
>> as-decimal-string 1.0 / 3.0
== "0.33"
>> as-decimal-string 2.0 / 3.0
== "0.67"

WARNING:  This is a quick-and-dirty sketch to show the kind of
calculations involved.  It is neither general (multiple precisions)
nor complete (e.g. doesn't handle negative values) nor maximally
efficient!

However, before belaboring the above code, is this the kind of
result you're looking for?

-jn-
```