Skip to content

Floating point precision for log10 #250

Open
@Jegge

Description

@Jegge

Hi there,

while experimenting with Uiua, I found that e.g. ₙ10 1000 gives the incorrect result of 2.9999999999999996 instead of 3, caused by a rounding error:

ₙ10 1 = 0
ₙ10 10 = 1 
ₙ10 100 = 2
ₙ10 1000 = 2.9999999999999996
ₙ10 10000 = 4
ₙ10 100000 = 5
ₙ10 1000000 = 5.999999999999999
ₙ10 10000000 = 7
ₙ10 100000000 = 8
ₙ10 1000000000 = 8.999999999999998

The following test case should uncover the problem:

∵(⍤"ₙ10 failed!" =ₙ10 ⁿ∶10 .) ⇡11

This error can be reproduced in the pad as well as in a locally installed interpreter.

It seems that there is an Issue with Rust's underlying f64/f32 log and that there exists an integer variant that circumvents/fixes the problem. However,

b.log(a),
seems to define the binary operation based on the floating point type f64. I suppose a possible solution would be a special implementation that explicitly uses ilog instead of log in the case that the supplied value is integral? Unfortunately I am a complete noob concerning Rust, so I can not provide you with a pull request. But I hope my analysis helps 😁

Jegge

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions