basvandijk / scientific

Arbitrary-precision floating-point numbers represented using scientific notation
BSD 3-Clause "New" or "Revised" License
73 stars 40 forks source link

the function scientific #7

Closed Lingyis closed 10 years ago

Lingyis commented 10 years ago

Using the routine scientific, while documented to use a user provided coefficient and exponent, should have the behavior that it returns a valid scientific number. I.e. currently:

Prelude Data.Scientific> let a = scientific 10 3
Prelude Data.Scientific> coefficient a
10
Prelude Data.Scientific> base10Exponent a
3

However, what a scientific number should return, is a coefficient of 1 and a base10Exponent of 4.

Not technically a bug, but it violates a user's expectation of a scientific number, since it is specifically stated in the documentation that the module uses scientific notation, with a link to the Wiki page.

The function should properly convert user input to an actual scientific notation number. Failing that, should create a new function like properScientific that creates a proper scientific number.

ibotty commented 10 years ago

Note, that that behavior breaks the recommended (see #6) way to check whether the scientific number is an integer. what about the following

scientific :: Integer -> Int -> Scientific
scientific c e = case quotRem c 10 of
                   (c', 0) -> scientific c' (e+1)
                   _       -> Scientific c e

and add a new function

uncheckedScientific :: Integer -> Int -> Scientific
uncheckedScientific = Scientific
basvandijk commented 10 years ago

The scientific function in scientific-0.3 now normalizes.

basvandijk commented 10 years ago

Just a heads up. I'm actually getting back on this. I now moved normalization from construction time (in the scientific function) to pretty-printing time (in toFloatDigits). The reason is performance: I don't want to normalize on construction each time.

I noted this in the documentation.