Skip to content

Floating point (f32) within a struct renders incorrecly #665

@JonoPrest

Description

@JonoPrest

Something is going wrong with floating point precision when rendering values inside a struct.

It works to render just the f32 value, and just to double check it also works to use rusts format implementation on the nested value.

But rendering nested struct value with hbs fails. The last assertion in this test fails with:

assertion left == right failed
left: "0.800000011920929"
right: "0.8"

    #[test]
    fn renders_nested_f32() {
        let hbs = Handlebars::new();

        let rendered_backoff_multiplicative = hbs
            .render_template(
                "{{backoff_multiplicative}}",
                &json!({"backoff_multiplicative": 0.8}),
            )
            .unwrap();
        assert_eq!(&rendered_backoff_multiplicative, "0.8");

        let sync_config = SyncConfig {
            initial_block_interval: 10_000,
            backoff_multiplicative: 0.8,
            acceleration_additive: 2_000,
            interval_ceiling: 10_000,
            backoff_millis: 5000,
            query_timeout_millis: 20_000,
        };

        assert_eq!(sync_config.backoff_multiplicative.to_string(), "0.8");

        let rendered_backoff_multiplicative = hbs
            .render_template(
                "{{backoff_multiplicative}}",
                &json!({"backoff_multiplicative": sync_config.backoff_multiplicative}),
            )
            .unwrap();

        //only this assertion fails
        assert_eq!(&rendered_backoff_multiplicative, "0.8");
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions