Playmaker Forum

PlayMaker Help & Tips => PlayMaker Help => Topic started by: jeaky on September 03, 2014, 10:35:20 AM

Title: Float add incorrect
Post by: jeaky on September 03, 2014, 10:35:20 AM
Hello, I'm creating a shop system, but Float add or Float operator still get incorrect result, such as 25 X 12.4 = 309.9999, not 310. Any help plz...
Title: Re: Float add incorrect
Post by: Lane on September 03, 2014, 10:40:50 AM
This is usually a rounding error in the variables and not a bug in the action's math. How do you get the values to begin with?
Title: Re: Float add incorrect
Post by: jeaky on September 03, 2014, 11:19:41 AM
Hi,Lane,I have used Float operator and converted the result to string.
Title: Re: Float add incorrect
Post by: Lane on September 03, 2014, 11:32:40 AM
Looks like a rounding error somewhere. I can repro, and it gets fractionally worse by increasing the count. Using Float Operator (Count * PriceEach = Total) it does calculate correctly, without rounding issues.

Just use Float Operator and Per Frame, rather than looping through incrementally. You could also force it to round the number up, I think the Math actions have a rounding action.
Title: Re: Float add incorrect
Post by: jeaky on September 03, 2014, 11:46:42 AM
Thanks, Lane. But how to change the result of "Int Add" to ""Count" variable, so I can set this variable to Float 1 in Float Operator.Thank you very much.
Title: Re: Float add incorrect
Post by: Lane on September 03, 2014, 12:15:42 PM
Use the Convert Int To Float action.
Title: Re: Float add incorrect
Post by: jeaky on September 03, 2014, 10:28:49 PM
Thanks,Lane. The Convert Int to Float action have same problem. 6*12.4=74.39999.
Title: Re: Float add incorrect
Post by: Lane on September 04, 2014, 06:59:56 AM
Are you looping through with Float Add or using Float Operator once/perframe? I could only get it to cause errors when looping through with the count.

If you're still having trouble here is a rounding action.
http://hutonggames.com/playmakerforum/index.php?topic=2073.msg9110#msg9110
Title: Re: Float add incorrect
Post by: jeaky on September 04, 2014, 07:45:15 AM
Yes, I looping through Float Operator every frame, I have read jeanfabre's code, but I don't know what the code means, it's hard to me.
Title: Re: Float add incorrect
Post by: Lane on September 04, 2014, 08:46:21 AM
Jean's post has an attachment, just download it and drag it into your project hierarchy, then you'll see float round in the action list.
Title: Re: Float add incorrect
Post by: jess84 on September 04, 2014, 08:51:33 AM
FYI -  not offering a solution, just a tip...  Turn on Debug ( checkbox under the fsm state area),  and you'll be down the values of your variables underneath each reference, it'll make it quicker and easier to see at what point things start going wrong. 
Title: Re: Float add incorrect
Post by: jeaky on September 04, 2014, 10:28:44 AM
Thanks, Lane. After use Float Round then 2*12.4=25 not equal to 24.8.
Hi, jess84, I have enabled DebugFlow, and seems havn't any error...
Title: Re: Float add incorrect
Post by: Lane on September 04, 2014, 10:52:04 AM
Here, I went ahead and made an FSM that does it for you. Download the unitypackage or follow the images.
Title: Re: Float add incorrect
Post by: jeaky on September 05, 2014, 02:53:48 AM
Thanks,Lane.Seems can't solve this problem, after you change the single price to 12.4, and the count set to 6, then result will be 74.39999.
Title: Re: Float add incorrect
Post by: Lane on September 05, 2014, 09:19:20 AM
That's really weird. I went from 1 - 200 it only messed up on the number 6, but changing the cost made it incorrect on other random count numbers as well.

You could do a custom rounding action for 2 decimals I think, but I can't make one while I'm out of town.

Report as bug with a repro scene.