max value addition odd behavior (near Infinity) #2858
-
Note: this is completely unimportant and impractical I am new to FP in general and I read in the docs there is no max int value and I was trying to determine what it is import gleam/io
import gleam/list
import gleam/int
import gleam/result
pub fn main() {
let ints = list.range(972, 1023)
let ints = list.map(ints, fn(x) { int.to_float(x) })
let part1 = list.fold(ints, 0.0, fn(count, e) { count +. result.unwrap(int.power(2, e), 0.0) })
io.debug(part1)
let part2 =
result.unwrap(int.power(2, 970.0), 0.0) +.
result.unwrap(int.power(2, 971.0), 0.0)
io.debug(part2)
io.debug(
part1 +.
result.unwrap(int.power(2, 970.0), 0.0) +.
result.unwrap(int.power(2, 971.0), 0.0)
)
io.debug(
part1 +.
result.unwrap(int.power(2, 971.0), 0.0) +.
result.unwrap(int.power(2, 970.0), 0.0)
)
io.debug(
part1 +.
part2
)
// max value calculate by hand, manual increment till it prints Infinity
io.debug(1.797693134862315807937289714053034150799341327100378269361737789804449682927647509466490179775e308)
} The ouput of the above code in the online repl is
I read the max value depends on system limits, my ram is two 8+8 GB sticks, x86_64 linux, running on firefox. I am curious why the above ordering matters in 'ok' line but not in the next two lines. I assumed it was 2^1024 - 1 but I was wrong, 2^1023 + 2^1022 + ... + 2^971 + ?? to do to get/compute the max value? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
JavaScript and Erlang have different number implementations. On Erlang ints have no max size, on JS it's a 64bit floating point number. |
Beta Was this translation helpful? Give feedback.
JavaScript and Erlang have different number implementations. On Erlang ints have no max size, on JS it's a 64bit floating point number.