Page 1 of 1

[0.13.3] [Rseding91] Smelting progress and rounding errors

Posted: Sun Jul 03, 2016 10:27 pm
by XKnight
These bugs may be splitted into separate reports, but I will post them here because they are related to each other.

Initial question: how many iron plates can be produced using 6 coal and a steel furnace?
Using math:
Energy from coal: 8 Mj (according to the game info) * 6 = 48 Mj
Iron smelting time: 3.5 sec (according to the game info)
Iron smelting time in steel furnace: 3.5 sec / 2 crafting speed = 1.75 sec
Energy consumption: 180 kW (according to the game info)
Putting it all together: 48 000 kJ / (1.75 sec * 180 kW) = 152.38 iron plate.
Using game: exactly 151 iron plate. What? Where is 152-nd plate?


Ok, Factorio, I don't know why, but you produces 151 iron plates for 6 coal.
But how many iron plates will you produce for 24 coal? 151*4=604?
Image
Image
Result = 603.99
Factorio, go home, you are drunk, you've eaten a small piece of my coal.

Actually, I know that 1/3 + 1/3 + 1/3 could be 0.99999, but this case is 1.000001 + 1.000001 + 1.000001 = 2.999999 - and it looks weird.

Re: [0.13.3] Smelting progress and rounding errors

Posted: Sun Jul 03, 2016 11:10 pm
by Rseding91
That's the nature of floating point numbers when you store them with some fixed amount of space :) If you can solve floating point rounding issues in computers I'm sure a large amount of people would love to know.

Until then, that's just how it is in Factorio.

Re: [0.13.3] Smelting progress and rounding errors

Posted: Sun Jul 03, 2016 11:44 pm
by XKnight
Rseding91 wrote:That's the nature of floating point numbers when you store them with some fixed amount of space :) If you can solve floating point rounding issues in computers I'm sure a large amount of people would love to know.

Until then, that's just how it is in Factorio.
(152.38-151)/152.38 = 0.9% this is an extermely huge rounding error.
I would believe that you are using 106 ticks unstead of 105 ticks (1.75 sec) for smelting, because this will give 48018 kJ energy, pretty close.

About the second part:
Assume, that 6 coals give enough energy for 151 iron plates and a small epsilon due to rounding errors, and this epsilon is definitely positive.
My question is how 4 positive epsilons may be negative?

Edit: By the way, 8000 Mj is an integer number, 180 kW = 180 kJ/sec = 3 kJ/tick = 3 kJ is an integer number, production time 105 or 106 ticks also is an integer number. Everything here can be represented in the integer numbers, and if you are using float C++ type it doesn't give any rounding errors within integer numbers and 2^23 values, because these numbers can be stored using only significand fields.
Anyway, these errors should have сumulative effect less than 1 production unit within 38 hours for float or 2M years for double.
So I think the second bug has the lowest ever priority, but the first bug doesn't look like rounding errors.

Re: [0.13.3] Smelting progress and rounding errors

Posted: Mon Jul 04, 2016 8:52 am
by XKnight
And again, I've wrote a small piece of code which simulates steel furnace behaviour (using float numbers) and it still gives exact results for 6 coal or 24 coal:
6 coal: http://codepad.org/L5BmSVz3 - 152 iron plates + 80/210 ~ 152.38 (as predicted by math)
24 coal: http://codepad.org/rIyBIrN4 - 609 iron plates + 110/210 ~ 609.52 (also predicted)
I am not asking to share your code, but it looks like you overrounded it.

Re: [0.13.3] Smelting progress and rounding errors

Posted: Mon Jul 04, 2016 10:15 am
by Rseding91
After looking into it there's no rounding error but there is a flaw in how the crafting machine consumes and produces items in regards to the energy used.

Each tick energy is consumed and applied to the progress of the current recipe. When the progress is >= required it's reset to 0 and items are given. That means a *little* bit of energy is lost with each recipe cycle.

I'll see if I can figure out a way to make it not drop that little bit.

Re: [0.13.3] [Rseding91] Smelting progress and rounding errors

Posted: Mon Jul 04, 2016 1:42 pm
by Rseding91
Fixed for 0.13.5.

Re: [0.13.3] [Rseding91] Smelting progress and rounding errors

Posted: Mon Jul 04, 2016 3:38 pm
by dee-
Rseding91 wrote:Fixed for 0.13.5.
Now this got me interested :)

Do you keep the energy overshot in the furnace as a "bonus" for the next item produced? Then how do you respect the fact you probably don't know what the next item will be (iron/copper/...) ?
Or do you transfer the overshot energy in the furnance back to the energy stock?

It matters because it impacts the production time. The former gets a small bonus which (I think) accumulates and results in a one-tick-faster-producted-item after several iterations.


By the way nice find XKnight ;) I'm so used to be drowned in bazillions of items I'd never accounted for these small number offsets. But it should be precise whereever possible, I agree, so thanks to the devs for being strict and fixing this one!

Re: [0.13.3] [Rseding91] Smelting progress and rounding errors

Posted: Mon Jul 04, 2016 4:56 pm
by XKnight
Rseding91 wrote:Fixed for 0.13.5.
Thank you alot!

I have only one last question about new implementation:
Image
What will happen if some item doesn't need a full tick to be finished? Will the rest of this tick be ignored or it will be used for the next item?
Also, can you clarify energy consumption in this case and production bonus.
Image

Thank you in advance.

Re: [0.13.3] [Rseding91] Smelting progress and rounding errors

Posted: Mon Jul 04, 2016 5:30 pm
by Rseding91
XKnight wrote:
Rseding91 wrote:Fixed for 0.13.5.
Thank you alot!

I have only one last question about new implementation:
Image
What will happen if some item doesn't need a full tick to be finished? Will the rest of this tick be ignored or it will be used for the next item?
Also, can you clarify energy consumption in this case and production bonus.
Image

Thank you in advance.
It ends up getting used for the next item. That was the bug that caused it to slowly use extra energy.

Re: [0.13.3] [Rseding91] Smelting progress and rounding errors

Posted: Mon Jul 04, 2016 11:22 pm
by dee-
Rseding91 wrote:It ends up getting used for the next item. That was the bug that caused it to slowly use extra energy.
So, hmmm, isn't this then creating the situation I described above with uneven timing between produced items because some get a headstart and falling into an earlier tick?

Re: [0.13.3] [Rseding91] Smelting progress and rounding errors

Posted: Mon Jul 04, 2016 11:48 pm
by Rseding91
dee- wrote:
Rseding91 wrote:It ends up getting used for the next item. That was the bug that caused it to slowly use extra energy.
So, hmmm, isn't this then creating the situation I described above with uneven timing between produced items because some get a headstart and falling into an earlier tick?
No, the total time spent comes out to exactly what the math would say.

With 6 coal and 200 iron ore in a steel furnace in 0.13.5 it produces exactly 152 iron plates and the furnace is sitting at 38% progress towards the 153rd plate after the exact ticks you would expect it to take based off the crafting time and speed of the furnace.

Re: [0.13.3] [Rseding91] Smelting progress and rounding errors

Posted: Mon Jul 04, 2016 11:56 pm
by XKnight
dee- wrote:
Rseding91 wrote:It ends up getting used for the next item. That was the bug that caused it to slowly use extra energy.
So, hmmm, isn't this then creating the situation I described above with uneven timing between produced items because some get a headstart and falling into an earlier tick?
Yes, it is.
If we need 1.5 ticks to produce one item, then first item will be produced at the second tick, while second item - at the third tick.
Image
But overall time spent is equal to the value predicted by math.

But I think this is much more better than current behaviour. Because all calculations and all perfect ratios don't account for one extra tick in production time.
For example, according to math we need 105 ticks to produce one iron plate in steel furnace, but in fact - 106 ticks.
At the same time for one steel plate we need 525 ticks, but real value is 526 ticks.
So we think that correct ratio is 1:1 (1 furnace for iron production and 1 furnace for steel production), but real ratio is 526:530.
And this difference is even bigger for quickly made items and for speed modules.
(for green circuit production using assembler III the difference is about 4%, with 4 speed modules inside - 12.5%, with 4 speed beacons around - 20.8%)

Re: [0.13.3] [Rseding91] Smelting progress and rounding errors

Posted: Tue Jul 05, 2016 1:14 am
by Zeblote
Rseding91 wrote:
dee- wrote:
Rseding91 wrote:It ends up getting used for the next item. That was the bug that caused it to slowly use extra energy.
So, hmmm, isn't this then creating the situation I described above with uneven timing between produced items because some get a headstart and falling into an earlier tick?
No, the total time spent comes out to exactly what the math would say.

With 6 coal and 200 iron ore in a steel furnace in 0.13.5 it produces exactly 152 iron plates and the furnace is sitting at 38% progress towards the 153rd plate after the exact ticks you would expect it to take based off the crafting time and speed of the furnace.
Does this also apply to other machines that craft stuff over time, like assemblers?

Re: [0.13.3] [Rseding91] Smelting progress and rounding errors

Posted: Tue Jul 05, 2016 9:36 am
by Rseding91
Yes, furnaces and assembling machines.

Re: [0.13.3] [Rseding91] Smelting progress and rounding errors

Posted: Tue Jul 05, 2016 12:47 pm
by dee-
XKnight wrote:
dee- wrote:
Rseding91 wrote:It ends up getting used for the next item. That was the bug that caused it to slowly use extra energy.
So, hmmm, isn't this then creating the situation I described above with uneven timing between produced items because some get a headstart and falling into an earlier tick?
Yes, it is.
If we need 1.5 ticks to produce one item, then first item will be produced at the second tick, while second item - at the third tick.
Image
But overall time spent is equal to the value predicted by math.

But I think this is much more better than current behaviour. Because all calculations and all perfect ratios don't account for one extra tick in production time.
For example, according to math we need 105 ticks to produce one iron plate in steel furnace, but in fact - 106 ticks.
At the same time for one steel plate we need 525 ticks, but real value is 526 ticks.
So we think that correct ratio is 1:1 (1 furnace for iron production and 1 furnace for steel production), but real ratio is 526:530.
And this difference is even bigger for quickly made items and for speed modules.
(for green circuit production using assembler III the difference is about 4%, with 4 speed modules inside - 12.5%, with 4 speed beacons around - 20.8%)
Alas, the perils of quantized time. :ugeek:

Hm... considering our own world this goes deep. Maybe our forum representations as inserters are a tongue-in-cheek remark by Wube knowing about The Higher Ones, which are playing a round of their favorite Universorio with us...

That aside :P


How about switching the time system of factorio to an event-based sorted list of upcoming game events with a linear approximation between the event points? That way a factory says "in now+1.0 realtime seconds summon a new iron plate and have 10 Watt consumed." Then the game can interpolate between "now" having a state of 100 plates and 500 W and "now+1.0" with 101 plates and 490 Watt seamlessy and with (for plates) or without any quantization (for power) . :ugeek: Shouldn't be too hard. :mrgreen: :twisted: :roll:

Okay, forget it. Thanks for the insights everyone! :)

Re: [0.13.3] [Rseding91] Smelting progress and rounding errors

Posted: Tue Jul 05, 2016 8:50 pm
by Case
Hey mates ..
I came here from the 0.13.5 release changes link. I always wondered why the numbers in the productivity tab not matched the theoretical numbers. I wrote a mod that reads and extracts the "crafting_progress" from the assembler entity and you could see the "rounding" (not sure if the right word here) and I thought well it is how it is.

Anyway, I'm posting here because I think the productivity modules have a related problem - I might entirely wrong on this but maybe you can help me out.


I have an Assembling machine 3 - with 12 beacons around all have speed 3 modules

Crafting Speed: 11.25 with 4 speed modules in the assembler
Crafting Speed: 8 with 4 productivity modules in the assembler

If you look at the attachments you see the production tab for it. The Speed module image shows the right numbers (according to theory) but the productivity modules should have -40% consumption products - because its the same amount of items produced.

4000 * (1-0.4) = 2400
1300 * (1-0.4) = 780

As you can see in the image the numbers come not even close even if I give it a bit giggle room for rounding
Nothing else consumes any iron-plates or copper-cables, the numbers you see are only from the electronic circuit production


I'm sure I oversee something obvious here, thats because I thought I ask it here.

If you need more data or anything else I could help to make clear what I mean - feel free to ask

Thanks
Case

Re: [0.13.3] [Rseding91] Smelting progress and rounding errors

Posted: Tue Jul 05, 2016 8:56 pm
by Rseding91
The stats window time views aren't 100% accurate. Especially so the smaller ones due to how "over time" stats are measured.

You need to measure exact counts using dedicated chests.

Re: [0.13.3] [Rseding91] Smelting progress and rounding errors

Posted: Tue Jul 05, 2016 9:01 pm
by Case
Hmm .. it shows the recent change just fine I can see that after the update to 0.13.5 I produce ~100 items more with my setup. But I will check this out and see what I get ..
thanks for the response

Re: [0.13.3] [Rseding91] Smelting progress and rounding errors

Posted: Tue Jul 05, 2016 10:03 pm
by Case
Yes with chests the item count is ok.

thanks