# \[RFC\] What should the arithmetic within the FixedPointNumbers be

**URL:** <https://discourse.julialang.org/t/rfc-what-should-the-arithmetic-within-the-fixedpointnumbers-be/46697>\
**Category:** Visualization\
**Tags:** package, colors, numerics\
**Created:** [September 16, 2020, 12:14pm UTC](https://discourse.julialang.org/t/rfc-what-should-the-arithmetic-within-the-fixedpointnumbers-be/46697 "2020-09-16T12:14:42Z")\
**Posts on this page:** 6\
**Page:** 4

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [May 19, 2023, 2:44pm UTC](https://discourse.julialang.org/t/rfc-what-should-the-arithmetic-within-the-fixedpointnumbers-be/46697/61 "2023-05-19T14:44:09Z")

</div>

> [@tim.holy](#):
>
> what type does `load("myimg.jpg")` return?

Couldn’t `load` take an optional argument indicating the desired return type?

For most other things in Julia, if you have a big library of code and you want to change the behavior, you pass in a different type. i.e. if I want to test the roundoff sensitivity of my floating-point algorithm, I might pass in some other precision like `BigFloat` and see how the numbers change, and I focus on writing my library in a type-generic way — you don’t have a global setting for the floating-point type. Similarly, if I want to check for integer overflow I might pass in a [`SafeInt`](https://github.com/JeffreySarnoff/SaferIntegers.jl). Why should fixed-point arithmetic be different?

---

<div class="post-metadata">

**Author:** ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)\
**Post date:** [May 20, 2023, 1:35am UTC](https://discourse.julialang.org/t/rfc-what-should-the-arithmetic-within-the-fixedpointnumbers-be/46697/62 "2023-05-20T01:35:23Z")

</div>

OK, we won’t use Preferences. I’m traveling so can’t check things most of the day, but I have my laptop now…I was remembering we had a `--checked` mode on `julia` itself, and was wondering why people were reacting to this so differently, but it doesn’t seem to be true. Maybe I was remembering very old Julia versions.

---

<div class="post-metadata">

**Author:** ![i9e1](https://avatars.discourse-cdn.com/v4/letter/i/ac91a4/32.png) [@i9e1](https://discourse.julialang.org/u/i9e1)\
**Post date:** [May 21, 2023, 3:14pm UTC](https://discourse.julialang.org/t/rfc-what-should-the-arithmetic-within-the-fixedpointnumbers-be/46697/63 "2023-05-21T15:14:16Z")

</div>

Some examples here boil my blood…  
To me it seems there is some confusion how fixed-point artihmetics work in general / why people are bothered with fixed-point at all.

example:

> [@Palli](#):
>
> We could actually do: N0f8 + N0f8 → N0f **9**

N0f8 + N0f8 → UQ1f8, we need one bit for b\* 2^(0) = b\*1  
Why should addition add another fractional bit which represents a resolution which was not in the operands? For normalized fixed points using wrap-around addition as currently implemented is also bad default without any mathematical meaning deduced from the **operands property** 0.0 \<= x \<= 1.0.

other example:

> [@tim.holy](#):
>
> ```julia
> acc = zero(eltype(list))
> for x in list
> acc += x
> end
> 
> ```

Assume we want to maintain highest precision. The needed type / size of `acc`  
than depends on our calculation order. (Fixed Points and Floating Points are both not associative, see gcc’s `-funsafe-math-optimizations` for floats, [FloatingPointMath - GCC Wiki](https://gcc.gnu.org/wiki/FloatingPointMath)) ) For a fully serial addition chain we need 1 extra bit per addition (Nel -1), so we can keep the potential overflow bit. A parallel tree-like scheme will only need one additional bit per stage, so the result will have log2(Nel) extra bits.  
Everything here depends on the input `list`!

What would be a usecase for this? This high-level generality will never be used in the wild by people concerned with stable and performant fixed-point implementations. Actually I consider this example in productive code **extremely dangerous**!  
(half-productive is ok: you are prototyping to see what your maximum accumulator size should be for some realistic inputs)

Agree that Fixed-Point-Numbers should behave as any other Julia number, but you have to keep in mind that they are not associative and have limitations on possible values and value range. They are no plug-in replacement for floats.

---

<div class="post-metadata">

**Author:** ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)\
**Post date:** [May 22, 2023, 10:49am UTC](https://discourse.julialang.org/t/rfc-what-should-the-arithmetic-within-the-fixedpointnumbers-be/46697/64 "2023-05-22T10:49:23Z")

</div>

> [@i9e1](#):
>
> What would be a usecase for this?

`mean(img)` where `img::RGB{N0f8}`. It’s used in the wild a lot. And no, we don’t initialize the accumulator this way, we use one with much higher precision. But you need to take care to do that. The example was just used to illustrate the perils of automatic widening.

---

<div class="post-metadata">

**Author:** ![i9e1](https://avatars.discourse-cdn.com/v4/letter/i/ac91a4/32.png) [@i9e1](https://discourse.julialang.org/u/i9e1)\
**Post date:** [May 26, 2023, 6:52pm UTC](https://discourse.julialang.org/t/rfc-what-should-the-arithmetic-within-the-fixedpointnumbers-be/46697/65 "2023-05-26T18:52:13Z")

</div>

see where this goes… I’m also not a fan of widening behavior by default.

As problematic as this loop is some code which uses splat `...` and some form of reduction.  
e.g.

```julia
sum(x...) = reduce(+, x...)
mean(x...)
norm(x...)
normalize(x...)
dot(x, y) # maybe critical

```

Possibly a few more. They are used a lot.  
Is there any problem in overloading these?  
The number of arguments is known when called and the accumulator can be chosen to achieve maximum accuracy. The return value is converted to the input type.  
This matches `mean(img)` as far as I understood.

And for everything else say clearly: “please implement the rest for yourself and think of limited range / accuracy”.

---

<div class="post-metadata">

**Author:** ![kimikage](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kimikage/32/14534_2.png) [@kimikage](https://discourse.julialang.org/u/kimikage)\
**Post date:** [May 11, 2024, 4:54am UTC](https://discourse.julialang.org/t/rfc-what-should-the-arithmetic-within-the-fixedpointnumbers-be/46697/66 "2024-05-11T04:54:12Z")

</div>

I apologize for the stalled development of FixedPointNumbers.jl.  
The discussion here has become somewhat dated.

We (or I) intend to control the arithmetic with OverflowContexts.jl instead of CheckedArithmetic.jl. Those packages were originally targeted for integer arithmetic, not for FixedPointNumbers.jl.

Even there, a decision is needed as to whether the division should be `checked` in the `@unchecked` context.  
Discussion is important, but making decisions is even more important.

> <https://github.com/JuliaMath/OverflowContexts.jl/issues/9>
>
> Julia's default integer division is checked. This occurs with division by 0, and… \`typemin(T) ÷ -one(T)\`.
> 
> LLVM deems either of these cases undefined behavior, which is very very bad because the compiler is allowed to do whatever it wants in that case. Also on Windows it may throw a Win32 exception that crashes Julia.
> 
> This is in contrast to +/-/\*/^/abs where overflow has a defined result of wrapping around. And for example, C# doesn't stop checking division in an unchecked context. So it makes sense to not automatically follow this.
> 
> However, we could consider an option passed to the \`unchecked\` macros that allows people to opt in to unchecked division.

[Previous page](https://discourse.julialang.org/t/rfc-what-should-the-arithmetic-within-the-fixedpointnumbers-be/46697.md?page=3)
