Git Product home page Git Product logo

Comments (2)

casevh avatar casevh commented on June 16, 2024

I think it is the correct behavior.

Let's display both the decimal and binary values:

from gmpy2 import get_context, div_2exp, mpfr, next_below

get_context().precision = 2
get_context().emin = -2
get_context().emax = 3

get_context().subnormalize = False
a = div_2exp(mpfr(3), 4)
b = next_below(mpfr(0.25))

get_context().subnormalize = True
c = div_2exp(mpfr(3), 4)
d = next_below(mpfr(0.25))

a, a.__format__('b')
b, b.__format__('b')
c, c.__format__('b')
d, d.__format__('b')

The output is:

(mpfr('0.19',2), '1.1p-3')
(mpfr('0.19',2), '1.1p-3')
(mpfr('0.25',2), '1p-2')
(mpfr('0.12',2), '1p-3')

For 'a' and 'b', the binary output uses two bits ("1.1") but the exponent is -3 which is less than 'emin'. If the exponent is restricted to 'emin', the binary output for 'a' and 'b' would be '0.11p-2'. But this requires 3 bits ("0.11"). Subnormalize needs to round to 2 bits. div_2exp follows the honors the rounding mode (round ties to even) so '0.11' is rounded to '1.0' or '1p-2'. 'next_below' just uses the next smaller so '0.11' becomes '0.10' or '1p-3'.

I think I got the analysis correct. But the subtleties floating point representations never cease to surprise me.

from gmpy.

ozankabak avatar ozankabak commented on June 16, 2024

I am closing this issue as @casevh gave an accurate analysis of what is going on -- there does not seem to be a bug here.

from gmpy.

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.