ekmett / ekmett/half

Double rounding issue with fromRational

Open
#41 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Haskell
Stars
18
Forks
9
PR merge metrics
No merged PRs in 30d

Description

Currently, `fromRational :: Rational -> Half` is implemented as a composition `(toHalf :: Float -> Half) . (fromRational :: Rational -> Float)`. Unfortunately, this implementation is not correctly-rounding (i.e. round to nearest).

For example, consider `0x1.005fff8p0`. The nearest values expressible in `Half` are `0x1.004p0` and `0x1.008p0`, so `0x1.004p0` should be returned by `fromRational 0x1.005fff8p0 :: Half`. However, the current situation is:

```haskell
ghci> showHFloat (fromRational 0x1.005fff8p0 :: Half) ""
"0x1.008p0"
```

Fixing this is easy, as `base` exposes [`fromRat :: RealFloat a => Rational -> a`](https://hackage.haskell.org/package/base-4.20.0.1/docs/Numeric.html#v:fromRat), which should be correctly-rounding.

Of course, neither [Haskell 2010](https://www.haskell.org/onlinereport/haskell2010/haskellch6.html#x13-1350006.4) nor current [GHC base](https://hackage.haskell.org/package/base-4.20.0.1/docs/Prelude.html#v:fromRational) specifies `fromRational` to be correctly-rounding, so this is not strictly a bug. Nevertheless, I think the behavior should be documented if we are not going to "fix" this.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.