Stream: beginners

Topic: Representing large integers (Fibonacci)


view this post on Zulip TheoTheTorch (Jun 10 2026 at 11:11):

Bonjour!
I want to write a program that can index into the fibonnaci number sequence, but as it turns out, they quickly get larger than even a U128. I know there aren't BigInt-s in the standard library or language for a good reason, but that begs the question, how do you recommend I make my custom version for this program to work?

I basically need a data type with lenght I can define. The two options that come to mind are Str and List(U128) with some custom definition of a function to become my + operator.

Do you see another option and what do you recommend?
Also, while I'm at it, is Dec a true decimal number or basically something like F64 ?

Thanks in advance 'x)

view this post on Zulip Anton (Jun 10 2026 at 12:01):

Hi Theo :)

Dec a true decimal number or basically something like F64 ?

Dec is a true decimal and will avoid the typical float issues.

with some custom definition of a function to become my + operator.

By the way, if you make a function named plus on your type, it will just work with +.

Do you see another option and what do you recommend?

I would recommend using a List(64) so that for addition you can convert them to U128 and the result will fit guaranteed.

view this post on Zulip Norbert Hajagos (Jun 10 2026 at 12:39):

Also, you'll be able to write tests for it easily, because we have special "constructors" for number literals.
You don't need to do MyBigNum.new(1). You can just do 1 if MyBigNum implements the appropriate methods. I don't know where it's discussed, but this is the first msg i have found that mentions them (it's the from digits method):
https://roc.zulipchat.com/#narrow/channel/395097-compiler-development/topic/number.20type.20inference/near/556715492

view this post on Zulip Richard Feldman (Jun 10 2026 at 13:19):

@TheoTheTorch check out https://github.com/roc-lang/roc/blob/185a692980d8d85fb06960aad25d1510cd30d450/docs/langref/numbers.md#custom-number-types

view this post on Zulip Richard Feldman (Jun 10 2026 at 13:19):

it's not merged yet but that should explain - lmk if you have questions about it!

view this post on Zulip TheoTheTorch (Jun 10 2026 at 13:21):

Oh wow I am reading that right away!

view this post on Zulip TheoTheTorch (Jun 10 2026 at 13:22):

You guys were not kidding about the friendly part :')

view this post on Zulip TheoTheTorch (Jun 10 2026 at 14:22):

Hey, I got stuck on figuring out how I can instantiate a type I am defining. Also, I am guessing a nominal type is better for something like this than an opaque type?

Bigbig :: List(U64).{
    plus = |a, _b| {
        a # TODO
    }
    minus = |a, _b| {
        a # TODO
    }
    from_numeral : Numeral -> Try([Bigbig, ..], [InvalidNumeral, ..])
    from_numeral = |literal|
        match U64.from_numeral(literal) {
            Err(_) => {
                Try.Err(InvalidNumeral())
            }
            Ok(value) => {
                Try.Ok(List(value))
            }
        }
}

from_numaral : Numeral -> Try([List(U64), ..], [InvalidNumeral, ..])
instead of what I want it to be.

view this post on Zulip Richard Feldman (Jun 10 2026 at 14:31):

opaque is better actually!

view this post on Zulip Richard Feldman (Jun 10 2026 at 14:32):

I'd do like Bigbig :: { digits : List(U64) }.{ ... }

view this post on Zulip Richard Feldman (Jun 10 2026 at 14:34):

and from_numeral should return Try(Bigbig, ...)

view this post on Zulip TheoTheTorch (Jun 10 2026 at 14:34):

Is Try.Ok() not a Try ?

view this post on Zulip TheoTheTorch (Jun 10 2026 at 14:40):

Oh, you mean the Bigbig should not be inside a union.

Bigbig :: { digits : List(U64) }.{
    plus = |a, _b| {
        a # TODO

    }
    minus = |a, _b| {
        a # TODO

    }

    from_numeral : Numeral -> Try(Bigbig, [InvalidNumeral, ..])
    from_numeral = |literal|
        match U64.from_numeral(literal) {
            Err(_) => {
                Try.Err(InvalidNumeral())
            }
            Ok(value) => {
                Try.Ok( {digits : List(value)} )

            }
        }

}

I feel I've gotten closer with this x)

view this post on Zulip TheoTheTorch (Jun 10 2026 at 14:46):

Oh, List literals are made like [1, 2, 3, 4], not List(1, 2, 3, 4).

view this post on Zulip TheoTheTorch (Jun 10 2026 at 14:51):

This now compiles:

from_numeral = |literal|
        match U64.from_numeral(literal) {
            Err(_) => {
                Try.Err(InvalidNumeral("ohno"))
            }
            Ok(value) => {
                Try.Ok({ digits: [value] })
            }
        }

But trying to actually use this has an interesting effect:

_lol : Bigbig
_lol = 1

Segfault caught: EXCEPTION_ACCESS_VIOLATION (segfault) (code 0xC0000005)
Dev backend execution error: error.DevEvaluatorFailed

view this post on Zulip Anton (Jun 10 2026 at 14:54):

I will make an issue

view this post on Zulip TheoTheTorch (Jun 10 2026 at 14:56):

In case it helps, this is the entire source related to this issue, that produces it when ran:

main! = |arg| {
    _lol : Bigbig
    _lol = 1
    Ok({})
}


Bigbig :: { digits : List(U64) }.{
    from_numeral : Numeral -> Try(Bigbig, [InvalidNumeral(Str), ..])
    from_numeral = |literal|
        match U64.from_numeral(literal) {
            Err(_) => {
                Try.Err(InvalidNumeral("ohno"))
            }
            Ok(value) => {
                Try.Ok({ digits: [value] })

            }
        }
}

view this post on Zulip TheoTheTorch (Jun 10 2026 at 14:59):

I'll take off to do some other things for today, but I eagerly await to be back for more Roc :)
Until then, take care everyone :)))
:croissant: :coffee: :scarf:

view this post on Zulip Anton (Jun 10 2026 at 14:59):

Can you share your OS?

view this post on Zulip Anton (Jun 10 2026 at 14:59):

I do not hit the issue on macos

view this post on Zulip TheoTheTorch (Jun 10 2026 at 14:59):

Windows 11

view this post on Zulip Anton (Jun 10 2026 at 16:56):

The Segfault does not happen with the latest release so I think it's already fixed

view this post on Zulip TheoTheTorch (Jun 11 2026 at 10:52):

Oh wow, I am sorry I cost you time figuring that out ':/
You are right!
Running irm https://roc-lang.org/install_roc.ps1 | iex again, I am no longer getting the segfault.
Does that always get the latest nightly release?

view this post on Zulip Anton (Jun 12 2026 at 10:51):

The script is not immediately updated after a nightly release, but it will rarely be more than 48 hours behind.

view this post on Zulip Anton (Jun 12 2026 at 10:51):

Oh wow, I am sorry I cost you time figuring that out ':/

All good :)


Last updated: Jun 16 2026 at 16:19 UTC