playMaker

Author Topic: Float add incorrect  (Read 2626 times)

jeaky

  • Playmaker Newbie
  • *
  • Posts: 11
    • View Profile
Float add incorrect
« on: September 03, 2014, 07: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...

Lane

  • Administrator
  • Hero Member
  • *****
  • Posts: 2487
  • Yup.
    • View Profile
    • Cleverous
Re: Float add incorrect
« Reply #1 on: September 03, 2014, 07: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?

jeaky

  • Playmaker Newbie
  • *
  • Posts: 11
    • View Profile
Re: Float add incorrect
« Reply #2 on: September 03, 2014, 08:19:41 AM »
Hi,Lane,I have used Float operator and converted the result to string.

Lane

  • Administrator
  • Hero Member
  • *****
  • Posts: 2487
  • Yup.
    • View Profile
    • Cleverous
Re: Float add incorrect
« Reply #3 on: September 03, 2014, 08: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.

jeaky

  • Playmaker Newbie
  • *
  • Posts: 11
    • View Profile
Re: Float add incorrect
« Reply #4 on: September 03, 2014, 08: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.

Lane

  • Administrator
  • Hero Member
  • *****
  • Posts: 2487
  • Yup.
    • View Profile
    • Cleverous
Re: Float add incorrect
« Reply #5 on: September 03, 2014, 09:15:42 AM »
Use the Convert Int To Float action.

jeaky

  • Playmaker Newbie
  • *
  • Posts: 11
    • View Profile
Re: Float add incorrect
« Reply #6 on: September 03, 2014, 07:28:49 PM »
Thanks,Lane. The Convert Int to Float action have same problem. 6*12.4=74.39999.

Lane

  • Administrator
  • Hero Member
  • *****
  • Posts: 2487
  • Yup.
    • View Profile
    • Cleverous
Re: Float add incorrect
« Reply #7 on: September 04, 2014, 03: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
« Last Edit: September 04, 2014, 04:01:30 AM by Lane »

jeaky

  • Playmaker Newbie
  • *
  • Posts: 11
    • View Profile
Re: Float add incorrect
« Reply #8 on: September 04, 2014, 04: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.

Lane

  • Administrator
  • Hero Member
  • *****
  • Posts: 2487
  • Yup.
    • View Profile
    • Cleverous
Re: Float add incorrect
« Reply #9 on: September 04, 2014, 05: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.

jess84

  • Hero Member
  • *****
  • Posts: 506
    • View Profile
Re: Float add incorrect
« Reply #10 on: September 04, 2014, 05: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. 

jeaky

  • Playmaker Newbie
  • *
  • Posts: 11
    • View Profile
Re: Float add incorrect
« Reply #11 on: September 04, 2014, 07: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...

Lane

  • Administrator
  • Hero Member
  • *****
  • Posts: 2487
  • Yup.
    • View Profile
    • Cleverous
Re: Float add incorrect
« Reply #12 on: September 04, 2014, 07:52:04 AM »
Here, I went ahead and made an FSM that does it for you. Download the unitypackage or follow the images.
« Last Edit: September 04, 2014, 07:55:33 AM by Lane »

jeaky

  • Playmaker Newbie
  • *
  • Posts: 11
    • View Profile
Re: Float add incorrect
« Reply #13 on: September 04, 2014, 11:53:48 PM »
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.

Lane

  • Administrator
  • Hero Member
  • *****
  • Posts: 2487
  • Yup.
    • View Profile
    • Cleverous
Re: Float add incorrect
« Reply #14 on: September 05, 2014, 06: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.