octave-bug-tracker
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Octave-bug-tracker] [bug #65584] Wrong result of bitshift() function


From: Markus Mützel
Subject: [Octave-bug-tracker] [bug #65584] Wrong result of bitshift() function
Date: Fri, 12 Apr 2024 05:08:57 -0400 (EDT)

Update of bug #65584 (group octave):

                  Status:                    None => Need Info              

    _______________________________________________________

Follow-up Comment #1:

The default number type of Octave is double precision floating point numbers.
Afaict, double precision floating point numbers have a mantissa of 52 bits.
When you are shifting significant bits out of the mantissa "strange" things
are going to happen.

See:
https://en.wikipedia.org/wiki/Double-precision_floating-point_format#IEEE_754_double-precision_binary_floating-point_format:_binary64

Maybe, I misunderstood what that function is supposed to do.
Which results did you expect?

Maybe, you'd like to use a different number type? E.g.:

>> bitshift(uint64(3), 51)
ans = 6755399441055744
>> bitshift(uint64(3), 52)
ans = 13510798882111488
>> bitshift(uint64(3), 53)
ans = 27021597764222976
>> bitshift(uint64(3), 54)
ans = 54043195528445952


But you'll still run into "strange" behavior when you run out of bits:

>> bitshift(uint64(3), 61)
ans = 6917529027641081856
>> bitshift(uint64(3), 62)
ans = 13835058055282163712
>> bitshift(uint64(3), 63)
ans = 9223372036854775808
>> bitshift(uint64(3), 64)
ans = 0


Or if you'd like to stay with double precision floating point numbers, you
could multiply with exponents of 2:

ans = 1.6888e+15
>> 3*2^50
ans = 3.3777e+15
>> 3*2^51
ans = 6.7554e+15
>> 3*2^52
ans = 1.3511e+16
>> 3*2^53
ans = 2.7022e+16
>> 3*2^54
ans = 5.4043e+16




    _______________________________________________________

Reply to this item at:

  <https://savannah.gnu.org/bugs/?65584>

_______________________________________________
Message sent via Savannah
https://savannah.gnu.org/




reply via email to

[Prev in Thread] Current Thread [Next in Thread]