Elusive problem with string.tofloat()
BlitzMax Forums/BlitzMax Programming/Elusive problem with string.tofloat()
| ||
There's been a couple of reports of a strange problem on a few Macs that run my TimelineFX software. After a lot of head scratching I've narrowed it down to that fact that for some reason strings are not being converted to floats properly. For example something like 0.999 would be converted to 0, but a value of 1 would convert ok, almost like it's converting to integer instead. This leads to lot's of effects not working properly because a lot of the values for them are set to 0. Unfortunately it works perfectly fine on my Mac so I can't recreate it at all. I've just tried compiling a new version where I've edited blitz_string.c to use strtod instead of atof just on the off chance that might make a difference, but I'm just wondering if anyone else has any ideas where I could look for clues? Could it be a language issue, as they've pointed out they're using a french version of mac os, and the only other person to report the problem was italian. It wouldn't be a decimal point thing would it? I know German's use a "," instead of a "." for a decimal point, or am I grabbing at straws here? I also asked them to download and try my vaders game and see if the effects work ok with that but it worked absolutely fine, so the mind's boggling at the moment! Any help appreciated! :) |
| ||
Ah, the lovely unpredictability of floats :-). Have you tried doubles instead? I don't think it has to do something with the delimiter. In the Netherlands we also use the notation of 0,999 instead of 0.999. And I've had no problems with this code: Local a:String = "0.999" Print a.ToFloat() Print a.ToDouble() Which results in: 0.999000013 0.99900000000000000 If I use Dutch notation (0,999) it converts to 0.00. |
| ||
I don't think it has to do something with the delimiter Going by what you say below, it could still be that... If I use Dutch notation (0,999) it converts to 0.00. If TimelineFX allows users to enter values, or reads values from the built-in Windows controls (which would use the separator chosen by the user via Windows' language settings), that might explain it. |
| ||
Hmm, I could try doubles I guess. It does allow you to enter values, but this is simply from reading values in an xml file. Maybe I could read the value twice, one with a "." and the other a "," and take the value that doesn't equal 0... But I still can't figure out why the example game works fine, as that loads the effects files in exactly the same way. Thanks for the suggestions so far! |
| ||
but this is simply from reading values in an xml file Maybe the XML-loading code is somehow to blame, respecting regional number formatting while raw BlitzMax ignores it. As an experiment you could try telling Windows to use the French/Dutch/whatever formatting and see if you then get the same behavior. EDIT: A bit of googling shows that XML files should be using the dot in any case, so my XML-loading guess is probably wrong. Last edited 2010 |
| ||
Maybe a Replace(str, ",", ".").ToFloat() could fix it. |
| ||
Well it seems that it's not ToString that's at fault it's actually the xml loading. I'm using libxml, a wrapper by Brucey, and it's somehow trying to be clever in being locale aware and parsing any numbers it finds accordingly. So numbers like 0.99292 are being converted to 0,00000. Not too helpful. Strangely it's only happening on some Macs it seems, and I've certainly sold a few to euro countries that use this formatting without any complaints. And if I set my Mac to french, and change the way it formats numbers on the OS I cannot recreate the error still. |
| ||
That's interesting. You could try contacting Brucey, but he seems to have vanished from these forums. His last post was fourteen weeks ago. |