# Pre-RFC: Dealing with broken floating point

**URL:** <https://internals.rust-lang.org/t/pre-rfc-dealing-with-broken-floating-point/2673>\
**Category:** language design\
**Created:** [September 22, 2015, 12:20pm UTC](https://internals.rust-lang.org/t/pre-rfc-dealing-with-broken-floating-point/2673 "2015-09-22T12:20:26Z")\
**Posts on this page:** 8\
**Page:** 2

<div class="post-metadata">

**Author:** ![wthrowe](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/wthrowe/32/1252_2.png) [@wthrowe](https://internals.rust-lang.org/u/wthrowe)\
**Post date:** [December 31, 2015, 9:40pm UTC](https://internals.rust-lang.org/t/pre-rfc-dealing-with-broken-floating-point/2673/21 "2015-12-31T21:40:28Z")

</div>

> [@hanna-kruppe](#):
>
> I vaguely recall learning that LLVM always ignores (and thus breaks) signalling NaNs in its optimizations. I don't have a source though

[LLVM bug 6050 "floating-point operations have side effects"](https://llvm.org/bugs/show_bug.cgi?id=6050) is at least close. It's about floating point exceptions rather than sNaNs, but I imagine the issues are closely related.

I've encountered that issue in another (non-Rust) project where code broke when compiled using clang.

---

<div class="post-metadata">

**Author:** ![hanna-kruppe](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/hanna-kruppe/32/6540_2.png) [@hanna-kruppe](https://internals.rust-lang.org/u/hanna-kruppe)\
**Post date:** [February 14, 2016, 11:19pm UTC](https://internals.rust-lang.org/t/pre-rfc-dealing-with-broken-floating-point/2673/22 "2016-02-14T23:19:50Z")

</div>

So today I learned about `cfg(target_feature="...")`. I’m kinda surprised that neither I nor anyone else in this thread knew about it, it already exists since July 2015, although it’s unstable. This attribute already covers pretty much everything the hypothetical `cfg(target_float="...")` would have covered (other than libm stuff which was more of a random side thought), and has the advantage of already existing. It also has the disadvantage of being so naive as to be basically useless ([https://github.com/rust-lang/rust/issues/31662](https://github.com/rust-lang/rust/issues/31662)), but whatever, that can be fixed.

The `cfg(float_is_broken)` alternative, on the other hand, seems less and less attractive every time I look at it. @wthrowe describes how LLVM will foil attempts at strict IEEE compatibility, the flag is a very coarse thing anyway, and now the problem it was intended to address already has another solution.

To me, that settles the topic I set out to address with this thread, though this thread has also generated other good ideas, like using openlibm!

---

<div class="post-metadata">

**Author:** ![dckc](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/dckc/32/1194_2.png) [@dckc](https://internals.rust-lang.org/u/dckc)\
**Post date:** [April 16, 2016, 6:05am UTC](https://internals.rust-lang.org/t/pre-rfc-dealing-with-broken-floating-point/2673/23 "2016-04-16T06:05:29Z")

</div>

Perhaps not directly related, but has anyone looked into a unum library for rust?

> **[REX-Computing/unumjl](https://github.com/REX-Computing/unumjl)**
>
> Julia Implementation of Unums. Contribute to REX-Computing/unumjl development by creating an account on GitHub.

https://www.slideshare.net/slideshow/embed_code/key/2XWUzNJW0O4V5f

---

<div class="post-metadata">

**Author:** ![eddyb](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/eddyb/32/8411_2.png) [@eddyb](https://internals.rust-lang.org/u/eddyb)\
**Post date:** [April 16, 2016, 10:35pm UTC](https://internals.rust-lang.org/t/pre-rfc-dealing-with-broken-floating-point/2673/24 "2016-04-16T22:35:47Z")

</div>

I have, after reading through the whole of “The End of Error” (I’d recommend it even if his plans have changed).

The problem with the unum 1.0 design is that to efficiently implement the scratchpad in software, you need a lot of type-level numerical computation, which Rust doesn’t have right now.

[unum 2.0](http://www.johngustafson.net/presentations/Multicore2016-JLG.pdf) is a significantly simplified design. However, my hunch is that they tried to do a _hardware_ implementation, or at least considered it, and found several limitations or points of unnecessary complexity which they addressed.

While an implementation based on lookup tables may work well in hardware, it can be difficult to optimize in software. Still, they may be useful for comparing with a hardware (FPGA?) implementation, for validity and benchmarking.

I haven’t had the time to put into an actual implementation (other than some old sketches I hadn’t pushed to GitHub, before realizing the type-level complexity involved), but I’ve discussed several points on IRC (#rust-offtopic).

I also got [the `unum` crate](https://crates.io/crates/unum) registered (in retrospect, prematurely).

If anyone wants to approach further development, I’d be glad to help with whatever I can.

---

<div class="post-metadata">

**Author:** ![sebk](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/sebk/32/2077_2.png) [@sebk](https://internals.rust-lang.org/u/sebk)\
**Post date:** [October 11, 2016, 1:23am UTC](https://internals.rust-lang.org/t/pre-rfc-dealing-with-broken-floating-point/2673/25 "2016-10-11T01:23:51Z")

</div>

After looking at the presentation and thinking a few hours about it:

It isn’t anything new. Basically the laws of exponentiation: exp(a \* b) = exp(a) + exp(b) exp(a ^ n) = n \* exp(a) The advantage: multiplication/division and exponentiation are simple. (const / linear error) The disadvange: addition and subtraction are horrible (exponential error)

Also the “Memory Wall”: You don’t read 64bits from memory. You(r cpu) read(s) at entire cache-lines. Then the cpu contains a few more units than just multiply-add. At least mine does.

A problem that “breaks unum math”: Anything with addition. Try the fibnoacci sequence forwards and backwards. you will not reach 1. not even close.

Two intersecting lines -\> simple algebra -\> exact solution.

Okay. enough of this vapourware. (and sorry for the massive amounts of typos… 3am)

---

<div class="post-metadata">

**Author:** ![eternaleye](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/eternaleye/32/3505_2.png) [@eternaleye](https://internals.rust-lang.org/u/eternaleye)\
**Post date:** [February 13, 2017, 10:42pm UTC](https://internals.rust-lang.org/t/pre-rfc-dealing-with-broken-floating-point/2673/26 "2017-02-13T22:42:39Z")

</div>

One thing I don’t see addressed in this thread, and which could potentially cause some of the approaches suggested to founder: Which _version_ of IEEE 754? 1985 or 2008? What about the errata Wikipedia states were being worked on in 2015? This is not an idle question - many CPU architectures (such as x86\_64) define their behavior by the 1985 standard, but at very least RISC-V and AArch64 use the 2008 standard.

In addition, on the topic of unums, I suggest reading this paper arguing against them: [https://people.eecs.berkeley.edu/~wkahan/EndErErs.pdf](https://people.eecs.berkeley.edu/~wkahan/EndErErs.pdf)

---

<div class="post-metadata">

**Author:** ![DrizztVD](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/drizztvd/32/4521_2.png) [@DrizztVD](https://internals.rust-lang.org/u/DrizztVD)\
**Post date:** [July 2, 2018, 8:12am UTC](https://internals.rust-lang.org/t/pre-rfc-dealing-with-broken-floating-point/2673/27 "2018-07-02T08:12:30Z")

</div>

[http://elpub.bib.uni-wuppertal.de/servlets/DerivateServlet/Derivate-7505/dc1735.pdf](http://elpub.bib.uni-wuppertal.de/servlets/DerivateServlet/Derivate-7505/dc1735.pdf)

This provides a fairly extensive framework for testing conformity, it may just be what is needed.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [March 25, 2019, 8:25am UTC](https://internals.rust-lang.org/t/pre-rfc-dealing-with-broken-floating-point/2673/28 "2019-03-25T08:25:11Z")

</div>

Closed on March 25, 2019, 8:25am UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.

[Previous page](https://internals.rust-lang.org/t/pre-rfc-dealing-with-broken-floating-point/2673.md?page=1)
