float->int->string inconsistency

Monkey Forums/Monkey Bug Reports/float->int->string inconsistency

slenkar(Posted 2015) [#1]
Import mojo

Function Main:Int()
New bughunt
End Function

Const trev:Int=0
Const down:Int=1

Class bughunt Extends App

Method OnUpdate:Int()
Local myfloat:Float=2.6
Print String(Floor(myfloat))
Print String(Int(myfloat))
floatymethod(myfloat)


End Method

Method floatymethod:Void(param:Int)
Print String(param)
End Method

Method OnRender:Int()
End Method

Method OnCreate:Int()
SetUpdateRate 30
End Method
End Class




the string result after converting a float to int is different between targets
in html5 it is always 2
in glfw and android it can be 2.0

this makes a difference when using stringmaps for example, when you need a string to be precise


marksibly(Posted 2015) [#2]
This is a known issue and I wont be changing it.

The various primitive type conversions do differ in behaviour from target to target, and it would be too difficult (and potentially expensive) to make them all work identically.

If you need to format numbers in a consistent way, you will need to write some sort of formatting function. There isn't one in Monkey as yet.


Danilo(Posted 2015) [#3]
Function IntFunction:Void(param:Int)
    Print param
    Print String(param)
End

Function Main:Int()                     '  C++                    GLFW                   HTML
    Local myfloat:Float = 2.6           '
    Print myfloat                       '  2.6000000000000001     2.5999999046325684     2.6
    Print String(myfloat)               '  2.6000000000000001     2.5999999046325684     2.6
    Print Int(myfloat)                  '  2                      2                      2
    Print String(Int(myfloat))          '  2                      2                      2
    Print String(Floor(myfloat))        '  2.0                    2.0                    2      << .0 removed with HTML float and Floor()
    Print String(Int(Floor(myfloat)))   '  2                      2                      2
    IntFunction(myfloat)                '  2                      2                      2
                                        '  2                      2                      2

    Print String(3.0)                   '  3.0                    3.0                    3.0
    Print String(3.1)                   '  3.1                    3.1                    3.1

    Print String(Floor(3.0))            '  3.0                    3.0                    3      << .0 removed with HTML float and Floor()
    Print String(Floor(3.1))            '  3.0                    3.0                    3      << .0 removed with HTML float and Floor()
End

Floor() returns a Float, so output '2.0' and '3.0' is correct for C++/GLFW.
The only real difference I see is that HTML target removes the ".0" at the end - but only when used with Floor().
Int(Floor(myfloat)) outputs same on all targets, it is Integer.

First two results for Float is ordinary float precision thingy.


slenkar(Posted 2015) [#4]
I guess its not really a big enough issue to change monkey