About Monkey 2 › Forums › Monkey 2 Development › "+=" inconsistency.
This topic contains 5 replies, has 3 voices, and was last updated by
Danilo
1 year, 10 months ago.
-
AuthorPosts
-
June 3, 2017 at 12:57 pm #8445
when adding a float to an integer with “+=” gives odd results:
[/crayon]Monkey123456789101112131415161718[crayon-5cba15495b653371797777 inline="true" ]Namespace myapp#Import "<std>"Using std..Function Main()Local v:Vec2i = New Vec2iv.x += .1Print v.xLocal y:inty += .1Print yEndThis should print 0 for both but it displays some odd numbers and inconsistent at that.
It might be an issue for OSX Only.
I already posted it to GitHub. This is just in case somebody runs into this problem as well.
June 3, 2017 at 11:45 pm #8448I get 11598408 for both on Windows 10. I think it has to do with the Float to Int conversion done on the .1. If you use 0.1 instead or define y as a float, then you get correct results.
June 4, 2017 at 4:06 am #8449I get inconsistent results every time I run it:
this:
-1718160848
-1718160848
then this:
-1842821072
-1842821072
then this:
-1145541584
-1145541584etc..
The reason I run into this problem was because I was trying to get some integer values from a Rnd operation. and I couldn’t figure out why I was getting this strange values. I was getting this by adding the random float value to an integer.
June 4, 2017 at 5:52 am #8450It has nothing to do with ‘+=’, it is that ‘.1’ is not correctly converted to integer.
The result is a random (uninitialized) number:Monkey12345678910111213141516171819202122232425Function IntFunc( value:Int )Print valueEndFunction Main()For Local i:Int = 1 To 5IntFunc(.1) ' Shows the same value 5 timesNextPrint "----------"Local x:Int ' Display 5 times different valuex = .1Print xx = .1Print xx = .1Print xx = .1Print xx = .1Print xEndFor the loop it displays the same number 5 times, because it is only generated once.
For the Print statements, each of the 5 assignments result in different uninitialzed/random numbers.Output:
Monkey1234567891011121712979817129798171297981712979817129798-----------1467958384-1467957952-1467957504-1467957056-1467956608The generated C++ code has the wrong numbers directly implemented (hard coded):
C++1234567891011121314151617181920212223{bbInt l_i=1;for(;(l_i<=5);l_i+=1){g_default_IntFunc(4312097094);}}bb_print(bbString(L"----------",10));bbInt l_x{};l_x=140538451901328;bb_print(bbString(l_x));l_x=140538451901760;bb_print(bbString(l_x));l_x=140538451902208;bb_print(bbString(l_x));l_x=140538451902656;bb_print(bbString(l_x));l_x=140538451903104;bb_print(bbString(l_x));}void g_default_IntFunc(bbInt l_value){bb_print(bbString(l_value));}June 4, 2017 at 2:58 pm #8453I see. But strange still.
I was going to post the exact code but I have since modified it and can’t exactly remember how I had it that it was giving me wrong values with a Rnd assignment to an integer. I tried to duplicate it but can’t do it anymore. It wasn’t a constant value.
Oh well it will probably will creep in some other project again.June 4, 2017 at 4:16 pm #8454> But strange still.
Of course still a bug that needs to be looked at by Mark.
Works with ‘0.1’, but with ‘.1’ there is a problem somewhere
in the semantic process, automatic type conversion, or
code generation. -
AuthorPosts
You must be logged in to reply to this topic.