[0.13.3] [Rseding91] Smelting progress and rounding errors

This subforum contains all the issues which we already resolved.
XKnight
Filter Inserter
Filter Inserter
Posts: 329
Joined: Thu May 28, 2015 10:40 pm
Contact:

[0.13.3] [Rseding91] Smelting progress and rounding errors

Post 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.

Rseding91
Factorio Staff
Factorio Staff
Posts: 14093
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: [0.13.3] Smelting progress and rounding errors

Post 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.
If you want to get ahold of me I'm almost always on Discord.

XKnight
Filter Inserter
Filter Inserter
Posts: 329
Joined: Thu May 28, 2015 10:40 pm
Contact:

Re: [0.13.3] Smelting progress and rounding errors

Post 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.

XKnight
Filter Inserter
Filter Inserter
Posts: 329
Joined: Thu May 28, 2015 10:40 pm
Contact:

Re: [0.13.3] Smelting progress and rounding errors

Post 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.

Rseding91
Factorio Staff
Factorio Staff
Posts: 14093
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: [0.13.3] Smelting progress and rounding errors

Post 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.
If you want to get ahold of me I'm almost always on Discord.

Rseding91
Factorio Staff
Factorio Staff
Posts: 14093
Joined: Wed Jun 11, 2014 5:23 am
Contact:

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

Post by Rseding91 »

Fixed for 0.13.5.
If you want to get ahold of me I'm almost always on Discord.

dee-
Filter Inserter
Filter Inserter
Posts: 416
Joined: Mon Jan 19, 2015 9:21 am
Contact:

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

Post 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!

XKnight
Filter Inserter
Filter Inserter
Posts: 329
Joined: Thu May 28, 2015 10:40 pm
Contact:

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

Post 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.

Rseding91
Factorio Staff
Factorio Staff
Posts: 14093
Joined: Wed Jun 11, 2014 5:23 am
Contact:

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

Post 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.
If you want to get ahold of me I'm almost always on Discord.

dee-
Filter Inserter
Filter Inserter
Posts: 416
Joined: Mon Jan 19, 2015 9:21 am
Contact:

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

Post 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?

Rseding91
Factorio Staff
Factorio Staff
Posts: 14093
Joined: Wed Jun 11, 2014 5:23 am
Contact:

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

Post 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.
If you want to get ahold of me I'm almost always on Discord.

XKnight
Filter Inserter
Filter Inserter
Posts: 329
Joined: Thu May 28, 2015 10:40 pm
Contact:

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

Post 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%)

Zeblote
Filter Inserter
Filter Inserter
Posts: 973
Joined: Fri Oct 31, 2014 11:55 am
Contact:

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

Post 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?

Rseding91
Factorio Staff
Factorio Staff
Posts: 14093
Joined: Wed Jun 11, 2014 5:23 am
Contact:

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

Post by Rseding91 »

Yes, furnaces and assembling machines.
If you want to get ahold of me I'm almost always on Discord.

dee-
Filter Inserter
Filter Inserter
Posts: 416
Joined: Mon Jan 19, 2015 9:21 am
Contact:

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

Post 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! :)

Case
Long Handed Inserter
Long Handed Inserter
Posts: 57
Joined: Tue Jun 28, 2016 9:25 am
Contact:

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

Post 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
Attachments
Electronic Circuits with Speed Modules
Electronic Circuits with Speed Modules
Speed_Modules.jpg (485.78 KiB) Viewed 10252 times
Electronic Circuits with Productivity Modules
Electronic Circuits with Productivity Modules
Productivity_Modules.jpg (502.39 KiB) Viewed 10252 times

Rseding91
Factorio Staff
Factorio Staff
Posts: 14093
Joined: Wed Jun 11, 2014 5:23 am
Contact:

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

Post 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.
If you want to get ahold of me I'm almost always on Discord.

Case
Long Handed Inserter
Long Handed Inserter
Posts: 57
Joined: Tue Jun 28, 2016 9:25 am
Contact:

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

Post 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

Case
Long Handed Inserter
Long Handed Inserter
Posts: 57
Joined: Tue Jun 28, 2016 9:25 am
Contact:

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

Post by Case »

Yes with chests the item count is ok.

thanks

Post Reply

Return to “Resolved Problems and Bugs”