Giter VIP home page Giter VIP logo

Comments (2)

Julian-J-S avatar Julian-J-S commented on September 24, 2024

Hi @kskalski

this is working correctly and has to do with floating point imprecision.

pl.Config.set_fmt_str_lengths(100)

pl.DataFrame(
    {
        "f32": [132.04],
        "f64": [132.04],
    },
    schema_overrides={"f32": pl.Float32},
).with_columns(
    f32_long=pl.col("f32").map_elements(lambda x: f"{x:.20f}"),
    f64_long=pl.col("f64").map_elements(lambda x: f"{x:.20f}"),
)

# shape: (1, 4)
# ┌────────────┬────────┬──────────────────────────┬──────────────────────────┐
# │ f32        ┆ f64    ┆ f32_long                 ┆ f64_long                 │
# │ ---        ┆ ---    ┆ ---                      ┆ ---                      │
# │ f32        ┆ f64    ┆ str                      ┆ str                      │
# ╞════════════╪════════╪══════════════════════════╪══════════════════════════╡
# │ 132.039993 ┆ 132.04 ┆ 132.03999328613281250000 ┆ 132.03999999999999204192 │
# └────────────┴────────┴──────────────────────────┴──────────────────────────┘

python uses the f64 version internally with you can easily check

f'{132.04:.20f}'
# '132.03999999999999204192'

so in the end you are comparing ever to slightly different numbers.
This is expected because there needs to be some differencte between f64 and f32 otherwise we could always use f32 😉

from polars.

kskalski avatar kskalski commented on September 24, 2024

Oh, you are right, 312.04 is not representable precisely by whatever precision floating point number, since there is no rational number representation a/b where b=2^c for any c, as floating point numbers use exponent of base 2. The differences observed come from the way those numbers are rounded for display.

from polars.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.