Skip to content
This repository has been archived by the owner on Oct 22, 2024. It is now read-only.

Remove floating point string formatting for multiplayer multiplatform compatibility, fixes issue #34 #47

Merged
merged 2 commits into from
Feb 14, 2016

Conversation

Afforess
Copy link
Collaborator

The only sensor using floating points was the evolution factor and pollution around player sensors. Day time, kill count, player locations, and play time all used integers already.

I believe the main problem with floats is that the string.format (which essentially just calls the system library printf or similar) is platform-dependent. This is supported by FFF 108: https://www.factorio.com/blog/post/fff-108 . The gist of the problem, as I understand it, is that the system library may return differing values for Linux, Mac, and Windows. However, there is no differing return values for integers, so doing mathematical trickery to break a floating point number into two integers (the whole number component and fractional decimal component), and printing a floating number as a string made up of two integers should side-step the platform-dependency issues. This fixes #34 [reformatted for auto-close -Narc].

Possibly in the future when Factorio moves to LuaJIT, this will go away (as LuaJIT has it's own platform-independent string formatter), but for now, this code is needed.

@narc0tiq
Copy link
Owner

I'm not entirely sure FFF 108 is a good reference, in as much as it explicitly refers to string.format("%g", some_float) (i.e., the implicit string format used when none is given). Specifying with things like %0.4f should still be safe.

That said, I'm going to take this one, if only because you already did the work and it has the potential to help.

narc0tiq added a commit that referenced this pull request Feb 14, 2016
Remove floating point string formatting for multiplayer multiplatform compatibility, fixes issue #34
@narc0tiq narc0tiq merged commit 301845c into narc0tiq:master Feb 14, 2016
end

return {self.format_key, string.format("%0.1f%%", game.evolution_factor * 100)}
return {self.format_key, string.format("%d.%d%%", whole_number, fractional_component)}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Via https://forums.factorio.com/viewtopic.php?p=136685#p136685:

If percent_evo_factor is 1.0215 then fractional_component is 215 and the string.format shows an obviously wrong number.

Fixed in #52.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Rip out the floating points by the short and curlies
2 participants