Skip to content

ConfigFile saving floats with low precision and unable to handle floats > 1e+06 #82424

@RolandMarchand

Description

@RolandMarchand

Godot version

v4.2.dev.custom_build [2048fe5]

System information

Godot v4.2.dev (2048fe5) - Gentoo 2.14 - Wayland - Vulkan (Forward+) - dedicated AMD Radeon RX 6600 (RADV NAVI23) () - AMD Ryzen 5 5600 6-Core Processor (12 Threads)

Issue description

There is an issue with the way ConfigFile encodes floating-point values. The low encoding precision is limited to the 6 biggest numbers, and since Godot's floating-point number is 32 bits, its ConfigFile encoding should fail at a minimum of 1e+14.

Examples

  • 123456.0 gets encoded as "123456.0," and parsed to 123456.0, but the number 1234567.0 will be encoded as "1.23457e+06," and parsed to 1234570.0, which is wrong.

  • 11111111111111.0, gets encoded as "1.11111e+13," and parsed to 11111100000000.0.

Proposed Fix

I suggest to only encode floats greater than 1e+14 in scientific notation, and to keep 14 digits of precision when doing so, while omitting trailing 0's. Otherwise, standard notation should be used.

Note

This is potentially a good issue to receive the label "good first issue."

Steps to reproduce

extends Node2D

func _ready():
	var cfg = ConfigFile.new()
	cfg.set_value("section", "key", 1000001.0)
	# The issue only occurs when parsing encoded text,
	# such as with `parse()` or `load()`
	cfg.parse(cfg.encode_to_text())
	assert(cfg.get_value("section", "key") == 1000001.0)

Minimal reproduction project

godot.zip

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions