Skip to content

Add formatting of small floats (10^-n)#319

Open
Aras14HD wants to merge 1 commit intotolik518:masterfrom
Aras14HD:format-float-small
Open

Add formatting of small floats (10^-n)#319
Aras14HD wants to merge 1 commit intotolik518:masterfrom
Aras14HD:format-float-small

Conversation

@Aras14HD
Copy link
Collaborator

This enables formatting small floats like 6.631537423e-34 as 6.631537423 × 10^-34, by allowing negative exponents in format_float (and some more logic to handle that).

Also fixes #315

Resolves #308

also fixes incorrect rounding
@tolik518
Copy link
Owner

tolik518 commented Feb 24, 2026

On your test-run factorion replied on 6.631537423e-34! with Factorial of 6.631537423 × 10-34 is approximately 1, before that it was replying with Factorial of 0.000000000000000000000000663154 is approximately 0,999999999999999999999999617217.

https://www.reddit.com/r/testingground4bots/comments/1rabfbh/comment/o77a0e8/

The before reply was more precise

@Aras14HD
Copy link
Collaborator Author

Ok investigating this I found, that the true result is 9.999999999999999999999999999999996172172717063657224823672597116605474397837555828144330523967376628717255627642017055031081750434013261087338778618778539329115394750197229780734699344408893837837414084874090262554352460558789098273872863113806646310596473029774819683214882891955307493169687938970352854185035e-1 not 0.999999999999999999999999617217. That's a lot more 9s in reality (more than 30).

Even though I confirmed we now do better (as more correct), I wanna figure out why it was wrong previously.

@Aras14HD
Copy link
Collaborator Author

Checked on master now, and can't reproduce that response, just get 0.1. What options (consts like float_precision) did you use?

@tolik518
Copy link
Owner

Checked on master now, and can't reproduce that response, just get 0.1. What options (consts like float_precision) did you use?

I needed a while to reproduce, but it happens to me only if the language is NOT en.

Example-test:

#[test]
fn teststuff() {
    let consts = Consts::default();
    let comment = Comment::new(
        "6.631537423e-34! ",
        (),
        Commands::TERMIAL,
        MAX_LENGTH,
        "de",
    )
        .extract(&consts)
        .calc(&consts);

    let reply = comment.get_reply(&consts);
    assert_eq!(
        reply,
        "Fakultät von 0,000000000000000000000000663154 ist ungefähr 0,999999999999999999999999617217 \n\n\n*^(Dieser Kommentar wurde automatisch geschrieben | [Quelltext](http://f.r0.fyi))*"
    );
}

for en the test would fail, for en the reply would have been Factorial of 0 is approximately 0.1 \n\n\n*^(This action was performed by a bot | [Source code](http://f.r0.fyi))* as you stated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Rounding error on 0.9999... Very small float formatting

2 participants