#How fix floating point errors in lua track?

19 messages · Page 1 of 1 (latest)

rigid sundial
boreal halo
#

Could you please share your code and the test details as text in a codeblock?

#

Images are hard to copy/paste from. Or even to read for some people!

low haloBOT
rigid sundial
#
local cars = {}

-- returns the amount of working cars produced by the assembly line every hour
function cars.calculate_working_cars_per_hour(production_rate, success_rate)
  return production_rate / 100 * success_rate
end

-- returns the amount of working cars produced by the assembly line every minute
function cars.calculate_working_cars_per_minute(production_rate, success_rate)
  return cars.calculate_working_cars_per_hour(production_rate, success_rate)/60
end

-- returns the cost of producing the given number of cars
function cars.calculate_cost(cars_count)
  return (cars_count // 10) * 95000 + (cars_count % 10) * 10000
end

return cars
#

my error is specifically with the first function, the 2nd and 3rd functions as far as i know will work correctly

boreal halo
#

What's the error? Do tests run? Which tests? What do they say?

rigid sundial
#
CODE RUN
assert.are.equal(0.0, cars_assemble.calculate_working_cars_per_hour(0, 100))
assert.are.equal(221.0, cars_assemble.calculate_working_cars_per_hour(221, 100))
assert.are.equal(340.8, cars_assemble.calculate_working_cars_per_hour(426, 80))
assert.are.equal(1398.92, cars_assemble.calculate_working_cars_per_hour(6824, 20.5))
assert.are.equal(0.0, cars_assemble.calculate_working_cars_per_hour(8000, 0))
TEST FAILURE
Expected objects to be equal.
Passed in:
(number) 340.79999999999995453
Expected:
(number) 340.80000000000001137
boreal halo
#

Now we have some details 😊

#
  1. Do you understand what the test is doing?
  2. Do you understand what the test is expecting and why?
  3. Do you understand what your code returns and how it differs?
  4. Do you understand why your code is returning what it returns?
rigid sundial
#

i want to know how to fix the floating point error im getting, as far as i can tell i would have to modify the test in order to accept an approx true instead of exact true

boreal halo
#

Everyone else who completed the exercise didn't modify the tests, so that's certainly not needed!

#

It might be worth trying to see if it matters if you do the multiplication before the division

rigid sundial
#
function cars.calculate_working_cars_per_hour(production_rate, success_rate)
  return production_rate * success_rate / 100
end
#

this did pass

#

weird, never thought about changing the order since i figured it wouldnt change how the float was calculated

#

thanks good sir was able to complete the exercise

boreal halo
#

floats are ... weird

#

You're very welcome