playMaker

Author Topic: Float add incorrect  (Read 4483 times)

jeaky

  • Playmaker Newbie
  • *
  • Posts: 11
Float add incorrect
« 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...

Lane

  • Administrator
  • Hero Member
  • *****
  • Posts: 2511
  • Mender of the past
    • Cleverous
Re: Float add incorrect
« Reply #1 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?
Products by Cleverous
|| Vault Core : Database
|| Vault Inventory : Multiplayer Inventory
|| Vault Attributes : Character Stats
|| That Hurt! : Dmg Floaties
|| Quinn : 3D

jeaky

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

Lane

  • Administrator
  • Hero Member
  • *****
  • Posts: 2511
  • Mender of the past
    • Cleverous
Re: Float add incorrect
« Reply #3 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.
Products by Cleverous
|| Vault Core : Database
|| Vault Inventory : Multiplayer Inventory
|| Vault Attributes : Character Stats
|| That Hurt! : Dmg Floaties
|| Quinn : 3D

jeaky

  • Playmaker Newbie
  • *
  • Posts: 11
Re: Float add incorrect
« Reply #4 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.

Lane

  • Administrator
  • Hero Member
  • *****
  • Posts: 2511
  • Mender of the past
    • Cleverous
Re: Float add incorrect
« Reply #5 on: September 03, 2014, 12:15:42 PM »
Use the Convert Int To Float action.
Products by Cleverous
|| Vault Core : Database
|| Vault Inventory : Multiplayer Inventory
|| Vault Attributes : Character Stats
|| That Hurt! : Dmg Floaties
|| Quinn : 3D

jeaky

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

Lane

  • Administrator
  • Hero Member
  • *****
  • Posts: 2511
  • Mender of the past
    • Cleverous
Re: Float add incorrect
« Reply #7 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
« Last Edit: September 04, 2014, 07:01:30 AM by Lane »
Products by Cleverous
|| Vault Core : Database
|| Vault Inventory : Multiplayer Inventory
|| Vault Attributes : Character Stats
|| That Hurt! : Dmg Floaties
|| Quinn : 3D

jeaky

  • Playmaker Newbie
  • *
  • Posts: 11
Re: Float add incorrect
« Reply #8 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.

Lane

  • Administrator
  • Hero Member
  • *****
  • Posts: 2511
  • Mender of the past
    • Cleverous
Re: Float add incorrect
« Reply #9 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.
Products by Cleverous
|| Vault Core : Database
|| Vault Inventory : Multiplayer Inventory
|| Vault Attributes : Character Stats
|| That Hurt! : Dmg Floaties
|| Quinn : 3D

jess84

  • Hero Member
  • *****
  • Posts: 515
Re: Float add incorrect
« Reply #10 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. 

jeaky

  • Playmaker Newbie
  • *
  • Posts: 11
Re: Float add incorrect
« Reply #11 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...

Lane

  • Administrator
  • Hero Member
  • *****
  • Posts: 2511
  • Mender of the past
    • Cleverous
Re: Float add incorrect
« Reply #12 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.
« Last Edit: September 04, 2014, 10:55:33 AM by Lane »
Products by Cleverous
|| Vault Core : Database
|| Vault Inventory : Multiplayer Inventory
|| Vault Attributes : Character Stats
|| That Hurt! : Dmg Floaties
|| Quinn : 3D

jeaky

  • Playmaker Newbie
  • *
  • Posts: 11
Re: Float add incorrect
« Reply #13 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.

Lane

  • Administrator
  • Hero Member
  • *****
  • Posts: 2511
  • Mender of the past
    • Cleverous
Re: Float add incorrect
« Reply #14 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.
Products by Cleverous
|| Vault Core : Database
|| Vault Inventory : Multiplayer Inventory
|| Vault Attributes : Character Stats
|| That Hurt! : Dmg Floaties
|| Quinn : 3D