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.
-
AuthorPosts
-
December 20, 2017 at 7:38 pm #12385
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 platformI 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.
December 21, 2017 at 8:06 am #12388I 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 = 18446744073709551615For 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 = 9223372036854775807some little tests would be enough to fix your different signed maxs
December 21, 2017 at 9:20 am #12389Thanks 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 :).
December 21, 2017 at 9:41 am #12390here’s a sample code to test signed integers
Monkey123456789101112131415161718192021222324252627282930313233343536373839404142Namespace myapp#Import "<std>"Using std..Function Main()Print "signed Byte test"Local l:ULong=Pow(2,7)-1Local b:Byte=lPrint bb+=1Print bPrint "signed Short test"l=Pow(2,15)-1Local s:Short=lPrint ss+=1Print sPrint "signed Int test"l=Pow(2,31)-1Local i:Int=lPrint ii+=1Print iPrint "signed Long test"'l=Pow(2,63)-1 this doesn't work probably due to Pow using Double thus caping somehowl=9223372036854775807 'note that there is a bug with ted2 and you can not enter larger litterals so for larger values you'll have to use StringToUlongLocal sl:Long=lPrint slsl+=1Print slEndDecember 21, 2017 at 8:15 pm #12400Thanks, 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
December 21, 2017 at 11:13 pm #12401there are bitwise operators! :
shl – shift left same as – C’s <<
shr – shift right -same as C’s >>
& – and
| – or
~ – xorDecember 21, 2017 at 11:34 pm #12402Very helpful :). Thanks, Jesse!
-
AuthorPosts
You must be logged in to reply to this topic.