Stream: contributing

Topic: Changing default numeric types


view this post on Zulip Yuki (Mar 28 2023 at 15:23):

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 !

view this post on Zulip Brendan Hansknecht (Mar 28 2023 at 15:30):

I would guess we want the same sort of upgrading, but with I128 to U128 if the value is too large

view this post on Zulip Brendan Hansknecht (Mar 28 2023 at 15:33):

@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.

view this post on Zulip Richard Feldman (Mar 28 2023 at 18:18):

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)

view this post on Zulip Richard Feldman (Mar 28 2023 at 18:19):

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:

view this post on Zulip Yuki (Mar 29 2023 at 01:20):

thank you ! I will keep on the issue !

view this post on Zulip Richard Feldman (Mar 29 2023 at 01:29):

awesome, thank you so much for working on it!

view this post on Zulip Yuki (Mar 29 2023 at 15:20):

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 ?

view this post on Zulip Richard Feldman (Mar 29 2023 at 15:39):

yeah we don't have implementations for them yet, but I'd like to!

view this post on Zulip Yuki (Mar 29 2023 at 15:44):

For now I have no idea how to do that yet but can I try ? That sounds very exciting !

view this post on Zulip Richard Feldman (Mar 29 2023 at 15:45):

absolutely, please do! that would be amazing! :smiley:

view this post on Zulip Yuki (Mar 29 2023 at 15:46):

Ok I'll work on it ! Thank you :big_smile:

view this post on Zulip Richard Feldman (Mar 31 2023 at 12:57):

@Yuki this might be useful when looking into implementing Num.cos for Dec: https://github.com/ifduyue/musl/blob/7d756e1c04de6eb3f2b3d3e1141a218bb329fcfb/src/math/__cos.c

view this post on Zulip Richard Feldman (Mar 31 2023 at 12:57):

I actually think there's a good chance this should be the Num.cos implementation for all Frac types :thinking:

view this post on Zulip Folkert de Vries (Mar 31 2023 at 12:58):

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

view this post on Zulip Richard Feldman (Mar 31 2023 at 12:58):

oh interesting

view this post on Zulip Folkert de Vries (Mar 31 2023 at 13:00):

see https://www.youtube.com/watch?v=vAcf6d26kiM

view this post on Zulip Richard Feldman (Mar 31 2023 at 13:00):

but yeah if Num.cos was implemented in pure Roc, then:

view this post on Zulip Folkert de Vries (Mar 31 2023 at 13:01):

how do you mean "automatically" here?

view this post on Zulip Folkert de Vries (Mar 31 2023 at 13:01):

it won't magically work for dec. The constants are different between the different number types

view this post on Zulip Richard Feldman (Mar 31 2023 at 13:02):

ah, that's a shame

view this post on Zulip Richard Feldman (Mar 31 2023 at 13:02):

I assumed they would be the same

view this post on Zulip Folkert de Vries (Mar 31 2023 at 13:02):

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

view this post on Zulip Richard Feldman (Mar 31 2023 at 13:03):

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.

view this post on Zulip Richard Feldman (Mar 31 2023 at 13:04):

https://github.com/rutgers-apl/rlibm-32

view this post on Zulip Folkert de Vries (Mar 31 2023 at 13:04):

in my memory it's just 32-bit floats though

view this post on Zulip Folkert de Vries (Mar 31 2023 at 13:04):

yeah

view this post on Zulip Folkert de Vries (Mar 31 2023 at 13:04):

not sure if maybe at this point 64-bit is feasible

view this post on Zulip Folkert de Vries (Mar 31 2023 at 13:04):

but dec is 128-bit so

view this post on Zulip Folkert de Vries (Mar 31 2023 at 13:04):

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

view this post on Zulip Richard Feldman (Mar 31 2023 at 13:06):

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:

view this post on Zulip Richard Feldman (Mar 31 2023 at 13:06):

aside from having every entrypoint override them I guess

view this post on Zulip Richard Feldman (Mar 31 2023 at 13:10):

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

view this post on Zulip Richard Feldman (Mar 31 2023 at 13:38):

for reference, some different implementations: https://news.ycombinator.com/item?id=35386834

view this post on Zulip Yuki (Mar 31 2023 at 13:45):

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?

view this post on Zulip Richard Feldman (Mar 31 2023 at 13:45):

it can, or if you want you can implement it in Num.roc

view this post on Zulip Richard Feldman (Mar 31 2023 at 13:46):

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

view this post on Zulip Richard Feldman (Mar 31 2023 at 13:47):

since Dec uses fixed-point instead of floating-point, these may be useful:

view this post on Zulip Yuki (Mar 31 2023 at 13:50):

Oh I was looking around row-level part.. I’ll check Num.roc ! Thank you for reference links too.

view this post on Zulip Richard Feldman (Mar 31 2023 at 13:51):

absolutely, thanks for working on it! :smiley:


Last updated: Jul 05 2025 at 12:14 UTC