What the..

BlitzMax Forums/BlitzMax Programming/What the..

plash(Posted 2008) [#1]
SuperStrict

Framework brl.standardio

Local z:String = "--23"
Local x:Int = Int(z)
Print String(x)
Print String(Int(z))
Print String(Int("--23"))


Outputs:
0
0
23

Huh?


Grey Alien(Posted 2008) [#2]
haha, so clearly typecasting a string uses better parsing code than typecasting a variable.


TomToad(Posted 2008) [#3]
It's how things are being optimized by the compiler. On the last Print statement, the compiler sees that you are converting a literal string to an int, back to a string, then printing. So it optimizes by removing all of the conversion code and printing 23 directly (which is what the value of --23 actually is.). Since you store "--23" in a string variable for the other prints, the compiler can't optimize it and so the conversion is done during run time, and it can't come to grips with the double "--", so the conversion returns 0. You can see this by looking at the .s file the compiler generates.
Now I wonder why the compiler is able to convert "--23" properly, but the runtime modules can't.


plash(Posted 2008) [#4]
Hmm.. It probably uses faster code for runtime casting.. :(

It's highly unlikely that a user will use multiple signs on numbers, I just found this unusual..