开发者

String is wrong converted to float? (Blitzmax - Reflections)

I have a little Problem in Blitzmax. I try to read an INI-file and if I read floats they are converted in a very strange way. The line in the file which is concerned looks like that for example:

_fStrength=40.6

The Output of this looks like that:

DebugLog:_fStrength: 40.5999985

The code I use to开发者_如何学编程 read that works with reflection and looks like that:

For Local fld:TField = EachIn id.EnumFields()
            fld.Set(obj, SearchInFile("TempWeapon" + index, fld.Name(), "Weapons.ini"))
            DebugLog(fld.Name() + ": " + String(fld.Get(obj)))
        Next

I found out, that this only happens if the number after the "." does not equal's 5 or 0. I can't explain this behaviour, because if I do not use reflections, it works fine.

Could anyone help me please?


As you probably know, your computer stores numbers in binary code, using a limited size. 40.6 expanded in binary is a periodic sequence (101000.1001100110011001100..., infinitely), similarly to what happens when you try to write down the digits of 1/3) repeating and thus can not be represented exactly, so you get rounding errors.

The number of correct digits you get here looks like you are using single-precision floating point numbers, you can push the error further back by going to double, but it won't disappear.

As a reference, you might find Wikipedia on floating point helpful.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜