Jump to content

1.15 * 100 = 114


Recommended Posts

First of all, we got crash, while testing our mod:

0b481318bf51164ec371c04d956606d305e4a221

We look deeper and found interesting thing:

print ( math.floor ( 1.15 * 100 ))--114

You can type it in console. That's true.

math.floor ( 1.15 * 100 ) is 114.

 

So this Klei code does not work properly:

function InventoryItem:SetWalkSpeedMult(walkspeedmult)    local x = math.floor((walkspeedmult or 1) * 100)    assert(x >= 0 and x <= 255, "Walk speed multiplier out of range: "..tostring(walkspeedmult))    assert(walkspeedmult == nil or math.abs(walkspeedmult * 100 - x) < .01 , "Walk speed multiplier can only have up to .01 precision: "..tostring(walkspeedmult))    self.classified.walkspeedmult:set(x)end

Because x==114 and walkspeedmult * 100 == 115

Of course math.abs(walkspeedmult * 100 - x) < .01 is FALSE!

 

 

Edited by Maris
Link to comment
Share on other sites

Of course math.abs(walkspeedmult * 100 - x) < .01 is FALSE!

 No, it's not. If math.floor(1.15*100) == 114, then there's rounding error causing 1.15*100 to be 1.14999999999 or something similar. So x is 114, and walkspeedmult*100 is 1.1499999999, so math.abs(walkspeedmult * 100 - x) is 0.0099999999, which is less than 0.01. That being said, it shouldn't be crashing there, because you are giving it what should be valid input.

 

edit: derp, off by two orders of magnitude. that's really strange....

Edited by rezecib
Link to comment
Share on other sites

 No, it's not. If math.floor(1.15*100) == 114, then there's rounding error causing 1.15*100 to be 1.14999999999 or something similar. So x is 114, and walkspeedmult*100 is 1.1499999999, so math.abs(walkspeedmult * 100 - x) is 0.0099999999, which is less than 0.01. That being said, it shouldn't be crashing there, because you are giving it what should be valid input.

x = 114

walkspeedmult*100 = 114.9999999999

walkspeedmult*100 - x = 0.99999999

0.99999999 > 0.1

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
  • Create New...