# Revise \`std::num::Float\`

**URL:** <https://internals.rust-lang.org/t/revise-std-num-float/1606>\
**Category:** ideas (deprecated)\
**Created:** [February 16, 2015, 11:43pm UTC](https://internals.rust-lang.org/t/revise-std-num-float/1606 "2015-02-16T23:43:31Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![hauleth](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/hauleth/32/1322_2.png) [@hauleth](https://internals.rust-lang.org/u/hauleth)\
**Post date:** [February 16, 2015, 11:43pm UTC](https://internals.rust-lang.org/t/revise-std-num-float/1606/1 "2015-02-16T23:43:31Z")

</div>

What do you think about extracting math functions from `Float` into a different traits. Currently `Float` is pretty big and most of functions implemented there is just implementation of `cmath`. Also, according to doc, some of them has different meaning than “pure math” equivalents (`signum` or `is_positive`/`negative`).

Also it will provide way to utilize some of functions between `Int` and `Float` (i. e. there is no `max`/`min` implementation for `Int`).

As addition I would think about using `Float` as base for trigonometric functions. IMHO all languages do this that way and all of them do it wrong. There should be `Angle` enum that looks like:

```rust
enum Angle<T> {
    Rad(T),
    Deg(T),
    Grad(T)
}

```

With respectful method implemented on them. This will remove ambiguity on measurement units.

Also we should think about adding `Num` trait that will implement shared methods between `Float` and `Int` like:

- `min`/`max`
- `zero`
- `one`
- `min`/`max_value`
- `signum`
- `is_positive`/`negative`

It could be helpful (especially when we rethink some other traits relations) when implementing, i.e. interval arithmetic, complex numbers and other algebraic oddities that has similar properties as numbers.

---

<div class="post-metadata">

**Author:** ![Gankra](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/gankra/32/6002_2.png) [@Gankra](https://internals.rust-lang.org/u/Gankra)\
**Post date:** [February 17, 2015, 3:27am UTC](https://internals.rust-lang.org/t/revise-std-num-float/1606/2 "2015-02-17T03:27:38Z")

</div>

Have you read the num-reform RFC: [https://github.com/rust-lang/rfcs/blob/master/text/0369-num-reform.md](https://github.com/rust-lang/rfcs/blob/master/text/0369-num-reform.md)

and its discussion: [https://github.com/rust-lang/rfcs/pull/369](https://github.com/rust-lang/rfcs/pull/369)

?

This is what specified the current API.

---

<div class="post-metadata">

**Author:** ![bluss](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/bluss/32/2264_2.png) [@bluss](https://internals.rust-lang.org/u/bluss)\
**Post date:** [February 17, 2015, 8:23am UTC](https://internals.rust-lang.org/t/revise-std-num-float/1606/3 "2015-02-17T08:23:04Z")

</div>

A well-typed angle measurement is a good idea, but I think unifying the representation is more efficient:

```rust
// this is just a rough sketch. An angle is a wrapper around an internal storage of a value in radians.
struct Angle<F>(F);

impl<F> Angle<F> for F: Float {
    fn radians(x: F) -> Self { Angle(x) }
    fn degrees(d: F) -> Self { Angle::radians(d * Float::pi() / 180.) } /* do the right cast here etc */
}

```

---

<div class="post-metadata">

**Author:** ![hauleth](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/hauleth/32/1322_2.png) [@hauleth](https://internals.rust-lang.org/u/hauleth)\
**Post date:** [February 17, 2015, 9:50am UTC](https://internals.rust-lang.org/t/revise-std-num-float/1606/4 "2015-02-17T09:50:50Z")

</div>

I was looking for that but I cannot found it. My bad.

According to topic, IMHO that RFC was step backwards as maybe it provided smaller set of traits but also it limited flexibility of code. There was said that it is hard to find examples of functions that are common for floats and integers. I.e. polynomials or linear algebra and PistonDevelopers/image is example where `Zero`, `One` and `max_value` neded to be reimplemented to write generic code.

For now `Float` looks like a god-trait that do everything. I would vote up for resplitting it into smaller, more reusable pieces. It would also make possible to implement some of this traits to SIMD vectors.

---

<div class="post-metadata">

**Author:** ![hauleth](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/hauleth/32/1322_2.png) [@hauleth](https://internals.rust-lang.org/u/hauleth)\
**Post date:** [February 18, 2015, 4:59pm UTC](https://internals.rust-lang.org/t/revise-std-num-float/1606/5 "2015-02-18T16:59:53Z")

</div>

Additional “pros” when moving math functions from `Float` to, i.e. `Math` trait:

The newcomers from other languages could still use “oldskul” syntax, i.e.: `Math::sin(0.0)` and at the same time, “proper” Rust syntax (`0.0.sin()`) would still work.

Win - Win

---

<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:23am UTC](https://internals.rust-lang.org/t/revise-std-num-float/1606/6 "2019-03-25T08:23:56Z")

</div>

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

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