exercism / euphoria

Exercism exercises in Euphoria.
https://exercism.org/tracks/euphoria
MIT License
3 stars 8 forks source link

grains exercise #54

Closed glennj closed 5 months ago

glennj commented 5 months ago
  1. The test file gives away the implementation

    test_equal("grains on square 64",square(64),power(2,63))
  2. I wanted to solve it with bitwise functions, but hit the 32-bit integer limit

    $ cat bits.e
    include std/math.e
    ? {30, shift_bits(1, -30)}
    ? {31, shift_bits(1, -31)}
    ? {32, shift_bits(1, -32)}
    
    $ eui bits.e
    {30,1073741824}
    {31,2147483648}
    {32,0}

    This exercise could use an instructions.append.md talking about Euphoria's number types.

axtens commented 5 months ago
test_equal("grains on square 64",9223372036854775808, square(64))        

fails with

failed: grains on square 64, expected: -9.22337203685478e+18 but got: 9.22337203685478e+18

Hey @ghaberek @petelomax why?

petelomax commented 5 months ago

See https://openeuphoria.org/forum/137540.wc#137540

I've fixed this in Phix so I don't care, so there :-)

glennj commented 5 months ago

2^63 is too big an integer for euphoria to handle. Big floats are OK though:

test_equal("grains on square 64", 9223372036854775808.0, square(64))        

passes

  passed: grains on square 64

I am surprised Euphoria doesn't automagically promote the too-big-int to a float.

petelomax commented 5 months ago

shift_bits() is defined in std/math.e and for reasons no doubt lost in the mists of time (and nothing to do with me!) it insists on and_bits(source_number, 0xFFFFFFFF) not just once but twice, which will obviously limit what it can return and probably mess up the sign bit (though that may just be a Phix thing). My advice would be to take a (renamed) copy of that to play with.

While it won't help you, Phix has builtin << and >> operators, implemented as lhs*power(2,rhs) and floor(lhs/power(2,rhs)) respectively, which are not only a tad more efficient by being inlined, but also don't impose any 32-bit limits. By inlined I just mean the front-end emits the exact same IL it would were the longhand versions found in the source code.

axtens commented 5 months ago

I am surprised Euphoria doesn't automagically promote the too-big-int to a float. Lots of rough edges on this language. Have a chat to @ghaberek about its long and circuitous history. Greg's working on v4.2. I'm expecting a lot of @petelomax's fixes for Phix will end up in the final product.

axtens commented 5 months ago

Addressed in https://github.com/exercism/euphoria/pull/61