#How fix floating point errors in lua track?
19 messages · Page 1 of 1 (latest)
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!
Increase your chance of getting help and look like a pro by sharing codeblocks not images. For example, you can type the following. Note, the ``` must be on their own line.
```
for number in range(10):
total += number;
```
Discord will render that as so:
for number in range(10):
total += number;
Click here to learn more about codeblocks: https://exercism.org/docs/community/being-a-good-community-member/writing-support-requests and http://bit.ly/howto-ask
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
What's the error? Do tests run? Which tests? What do they say?
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
Now we have some details 😊
- Do you understand what the test is doing?
- Do you understand what the test is expecting and why?
- Do you understand what your code returns and how it differs?
- Do you understand why your code is returning what it returns?
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
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
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