質問

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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top