Hi, I'm trying to fix for #2847.
I figured out how to change the default width of float but for integer, since we have the idea of RangedNumber
, I think I need to do more than float.
If I understand correctly, thanks to RangedNumber
, the number that does not fit to the default type can have a bigger numeric type and its default is I64 and the next is U64.
I think I can change this default to I128 but this means the above feature is going to be kind of pointless because I128 is the biggest numeric type and it is going to be the default.
So I'm wondering if I am doing the right thing in the first place ...
This is the draft PR.
https://github.com/roc-lang/roc/pull/5218
Thank you !
I would guess we want the same sort of upgrading, but with I128 to U128 if the value is too large
@Richard Feldman are you at all worried that this change for integer types will lead to a lot of misrepresentative benchmarks for roc. Where people do a math heavy benchmark with ints and are surprised it takes like 2x longer than other languages. Like for Dec, i think it won't be as much of an issue because the tradeoff is really clear why we might pick Dec by default. With I128, we really are just hitting performance without a clear reason and it would likely be unexpected by end users.
I'm not worried about it. I think the odds are higher that a beginner who isn't using type annotations runs into integer overflow and is surprised and doesn't realize that there's an easy fix (because for example they're a data scientist who is used to Python, which gracefully handles even huge numbers but doesn't have a "bigger integer type" to upgrade to)
also if someone says "I thought Roc was supposed to be fast but this code I wrote is really slow, why is that?" they'll very quickly get an answer I think :big_smile:
thank you ! I will keep on the issue !
awesome, thank you so much for working on it!
While fixing the failed tests caused by this change, I noticed some of the functions in Num module like Num.cos
doesn't support dec type. Changing the default of float to dec means we are going to support those functions with dec too, is that right ?
yeah we don't have implementations for them yet, but I'd like to!
For now I have no idea how to do that yet but can I try ? That sounds very exciting !
absolutely, please do! that would be amazing! :smiley:
Ok I'll work on it ! Thank you :big_smile:
@Yuki this might be useful when looking into implementing Num.cos
for Dec
: https://github.com/ifduyue/musl/blob/7d756e1c04de6eb3f2b3d3e1141a218bb329fcfb/src/math/__cos.c
I actually think there's a good chance this should be the Num.cos
implementation for all Frac
types :thinking:
do make sure we actually have a correct implementation. There is research from a couple of years ago that at the time, many standard implementations had subtle issues
oh interesting
see https://www.youtube.com/watch?v=vAcf6d26kiM
but yeah if Num.cos
was implemented in pure Roc, then:
F32
and F64
but also Dec
how do you mean "automatically" here?
it won't magically work for dec. The constants are different between the different number types
ah, that's a shame
I assumed they would be the same
which is why I've always thought this to be a fun bachelor/master thesis. it's narrowly scoped, but it's a real project
from that talk: :point_up:
If you go and download my GitHub library and test it out, my results would be more like 2x [the Intel math libraries]. I encourage you to check the results. We are faster. The moral of the story, we are not only correct, but also fast.
https://github.com/rutgers-apl/rlibm-32
in my memory it's just 32-bit floats though
yeah
not sure if maybe at this point 64-bit is feasible
but dec is 128-bit so
on the other hand we don't have rounding modes for Dec, and they may not even be that relevant for normal floats in roc
we don't support rounding modes in Roc, but in theory the host can set them and there's nothing Roc can realistically do about that :sweat_smile:
aside from having every entrypoint override them I guess
ok so maybe the answer here is actually just to focus on getting something working for Dec
, and then it's a separate consideration whether to change F32
and F64
for reference, some different implementations: https://news.ycombinator.com/item?id=35386834
- The CORE-MATH project: https://gitlab.inria.fr/core-math/core-math/-/blob/master/sr...
- The RLIBM project: https://github.com/rutgers-apl/The-RLIBM-Project/blob/main/l...
- The LLVM libc project: https://github.com/llvm/llvm-project/blob/main/libc/src/math...
Thank you! Ok I’ll reference that repository for Num.cos implementation when I get there.
I’m still trying to get familiar with roc compiler itself…
The actual implementation will go into a zig file(dec.zig) for bitcode, is that correct?
it can, or if you want you can implement it in Num.roc
you only need to use Zig if there are low-level primitive operations you don't have access to in Roc, but that probably shouldn't come up here
since Dec
uses fixed-point instead of floating-point, these may be useful:
Dec
(e.g. I don't think they use 128-bit)Oh I was looking around row-level part.. I’ll check Num.roc ! Thank you for reference links too.
absolutely, thanks for working on it! :smiley:
Last updated: Jul 05 2025 at 12:14 UTC