Maximum number?

About Monkey 2 Forums Monkey 2 Programming Help Maximum number?

This topic contains 6 replies, has 3 voices, and was last updated by  Abe _King_ 1 year, 3 months ago.

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #12385

    Abe _King_
    Participant

    Is there a way to get the maximum scalar value? Some languages provide Math.MAX_NUM or something like that. I would believe it could be very beneficial to have values like this built in so we don’t have to generate these ourselves…

    Does anyone know how? And preferably so 32 or 64 bit doesn’t make a difference? If one doesn’t exist I propose a module which can get the maximum numbers for each data type for 3 different values.

    something like this maybe?
    INT_MAX_32
    INT_MAX_64
    INT_MAX – adjusts to platform

    I know there are ways with some bit shifting techniques, like 1<<31 and such, but I can’t be too certain on what would be the best practice in this language! Thanks for your time.

    #12388

    abakobo
    Participant

    I think the max is always the same wether it uses one or two complement or magnitude..

    You could import limit.h and get the values from there? But C++ Int is not always 32 bit?

    Unsigned is easy because they are represented the same on all machines:
    UByte(8bits) Max = (2^8)-1 = 255
    UShort(16bits) Max = (2^16)-1 = 65535
    UInt(32bits) Max = (2^32)-1 = 4294967295
    Ulong(64bits) Max = (2^64)-1 = 18446744073709551615

    For signed it is usually one bit less but can be sometimes padded.

    if not padded (if padded remove one more bit?)

    Byte(8bits) Max = (2^7)-1 = 127
    Short(16bits) Max = (2^15)-1 = 32767
    Int(32bits) Max = (2^31)-1 = 2147483647
    Long(64bits) Max = (2^63)-1 = 9223372036854775807

    some little tests would be enough to fix your different signed maxs

    #12389

    Abe _King_
    Participant

    Thanks abakobo. That’s very helpful! I was considering importing limit.h as an option as well. . . Though, the main reason I didn’t was that I thought maybe there was a better way in M2 :).

    #12390

    abakobo
    Participant

    here’s a sample code to test signed integers

    #12400

    Abe _King_
    Participant

    Thanks, my friend! That’s very good work :). Interesting how that’s a bug…

    Curious as for why there are no bitwise operators! Usually, those are very handy in Game Development

    #12401

    Jesse
    Participant

    there are bitwise operators! :

    shl – shift left same as – C’s <<
    shr – shift right -same as C’s >>
    & – and
    | – or
    ~ – xor

    #12402

    Abe _King_
    Participant

    Very helpful :). Thanks, Jesse!

Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.