#bigi - BigInt library

1 messages Β· Page 1 of 1 (latest)

chrome dew
#

The Gleam Int type behaves differently in the Erlang and JavaScript targets. In the Erlang target it maps to Erlang's integer which is a "big integer", meaning it has arbitrary precision and can be as big as you need. In the JS target, Int maps to Number, which is a floating point value and has max/min values after which it is converted into Infinity, and additionally max/min safe values after which precision suffers.

To provide a consistent interface, I made the little Bigi package: https://hex.pm/packages/bigi. It works the same in both targets, using BigInt in the JS side. It has a rudimentary set of math operations implemented.

I mainly see this as a useful base for other packages that want to maintain correctness without imposing limits, and want to support both targets. If you are just targeting Erlang, you mostly don't need this, and if you are just targeting JS, you can also use the BigInt bindings in https://hex.pm/packages/plinth.

placid rover
#

Nice. At one point I worked quite a bit the BigInt ffi in gleam, but after quite a while I figured out I actually had to work with decimals (wanting to divide two large values and get a decimal value) and then on the JS side I actually had to work with strings to implement that kind of functionality, which then became a mess :p

#

A bit offtopic, but just in case you ever consider making a BigDecimal lib :d

chrome dew
#

It's not in the cards right now, because while this was just mostly bindings and thus easy to make, a decimal library probably isn't πŸ˜› I'm not a great maths person

#

Could take a look at how Decimal works in Elixir though

opaque sigil
#

I like it! If you could unify the language for floats too that would be nice. I made a proof of concepts with NIFs that try to emulate js-floats in Erlang, but there might be other options that work better.

chrome dew
#

What do you mean with "unify the language for floats"? I understand Erlang doesn't do things like NaN while JS does, do you mean that?

opaque sigil
#

Yes, that's exactly what I meant. πŸ˜…

chrome dew
#

I think that's above my skillset right now πŸ™‚ would require a lot of investigating

#

I took a look at Decimal too and while I think I could translate it to Gleam, I couldn't really fix any bugs in it or add new features because I don't know how it works. So I wouldn't be the best package owner.

#

now what I have been thinking about is a ranged integer library, to allow you to have unsigned ints and things like uint32, int8, etc etc. with optional overflow/underflow semantics

placid rover
#

there'll have to be a bigdecimal lib at some point ;)

chrome dew
#

maybe I should have made the negative exponent do integer division that many times? but since it rounds on every step, the result might be far from expected

chrome dew
#

Bigi 2.1.0 has been pushed with decoders from Dynamic and String, thanks to McNimble for the idea

chrome dew
#

Oh wow, looks like I've swapped the order of arguments in bigi.compare when compared to int.compare and friends from stdlib

#

no wonder my blog is ordering things confusingly!

#

Bigi 3.0.0 has been released, the only change is that the order of arguments for bigi.compare has changed.

chrome dew
#

Bigi 3.1.0 has been released. It contains added to_bytes and from_bytes functions to convert big integers to/from bit arrays (with adjustable endianness and signedness), courtesy of @primal brook. πŸ™‚

chrome dew
#

Oh, and added information to the docs on the Erlang big integer limits.

chrome dew
#

@hushed ibex I noticed you used bigi as a dependency in your package but had to FFI for some shifting functions. Do you think those should be in bigi directly? I could take a look at it.

#

it's curious that I failed to add left/right shift since they're directly supported in JS

hushed ibex
#

I think it would be useful to ensure that the bigi api matches the gleam int api. Adding the bit shifting to my code was a bit of a learning experience so I didn't think about creating an issue/pull request for bigi.

chrome dew
#

true, there's a lot of functions I didn't implement

#

I'll take a look at those when I have time

chrome dew
chrome dew
#

Bigi 3.2.0 has been released with the following changes:

+ Added functions to match gleam/int in stdlib:
  + bitwise_and
  + bitwise_exclusive_or
  + bitwise_not
  + bitwise_or
  + bitwise_shift_left
  + bitwise_shift_right
  + clamp
  + floor_divide
  + is_odd
  + max
  + min
  + negate
  + product
  + sum
  + undigits
hushed ibex
#

I appreciate you updating your useful library which has allowed me to remove the ffi from my library.

chrome dew
#

Np! Don't hesitate to contact me whenever you have wishes about my libraries. πŸ™‚

chrome dew
#

Bigi 4.0.0 has been released. This removes the decode(dyn) function and replaces it with a decoder() function for a more idiomatic decoding API. Thanks to @high void for noticing it and @static crater for pinging me. There's no need for creating your own decoder now.

static crater
#

Hero

chrome dew
#

now I just need to bump all my dependent libraries to use 4.0.0…

chrome dew
#

heads up @forest acorn you might check if you can relax the bigi dep in lenient_parse to include 4.0.0

#

also heads up @hushed ibex

forest acorn
chrome dew
#

GitHub..? πŸ€”

forest acorn
#

Oh damn

#

It’s not on github

chrome dew
#

why do you need a git dependency?

forest acorn
#

I’m thinking of the wrong thing too

#

Thinking of the float package you wrote

chrome dew
#

oh yeah (fixed not float 😏)

#

also heads up @median storm I see you depend on bigi, probably you can widen the dependency without issues (if you're not using the decode function)

forest acorn
#

What do you suggest for relaxing the dep version?

#

Maybe I can change bigi = ">= 3.2.0 and < 4.0.0" to just bigi = ">= 3.2.0

#

How do I update my local stuff to use the bigi 4.0.0? I removed that constraint and tried updating, but 3.2.0 remains. Gleam removing and adding also only brings in 3.2.0.

#

Oh, maybe my hardcoded version of pilkku is keeping it pinned

#

I'm not sure. πŸ€·β€β™‚οΈ

static crater
#

So bigi = ">= 3.2.0 and < 5.0.0"

chrome dew
#

yeah that one

#

I can't guarantee what break 5.0.0 would have

forest acorn
#

I'm able to relax that, but the gleam CLI still continues to install 3.2.0

lament halo
#

Maybe try gleam deps update?

forest acorn
#

oh! That worked! nice. I'm still bad at dep management.

#

Thank you!

lament halo
#

You're welcome!

forest acorn
#

Alas, now I have compilation issues in dev deps. First with gleam_json, which I relaxed as well in the same way, then in startest, which is using the latest version. Looks like the current dep restraints are set up such that we download all the latest versions of all deps now. 😭

lament halo
#

That's odd, might it be an issue with the stdlib?

forest acorn
#

Does startest need to be updated?

lament halo
#

I'd guess so

high void
#

Reminds me of Atom

forest acorn
#

Our founders built Atom previously

high void
wind nova
#

Works well

static crater
#

I use Zed all the time. Gleam support is top notch

median storm