Giter VIP home page Giter VIP logo

Comments (2)

mrzasa avatar mrzasa commented on July 30, 2024
  1. It happens only when precision is 0 (defailt), it works well when we define a non-zero precision:
> BigDecimal.mode(BigDecimal::EXCEPTION_INFINITY, false); BigDecimal::INFINITY.round(1)
# => Infinity
  1. With precision=0, #round converts a decimal to an integer and it's according to the docs

If n is specified and negative, at least that many digits to the left of the decimal point will be 0 in the result, and return value will be an Integer.
https://ruby-doc.org/stdlib-3.0.3/libdoc/bigdecimal/rdoc/BigDecimal.html#method-i-round

  1. For the source-divers, the error is raised here:
    if (round_to_int) {
    return BigDecimal_to_i(VpCheckGetValue(c));
    }

    because BigDecimal_to_i calls with always=1:
    GUARD_OBJ(p, GetVpValue(self, 1));

    and it means that the error for infinity is raised always, no matter what mode is set.

I believe it's an expected behaviour. All we could do is add a small note in the docs to be more explicit. WDYT @mrkn ?

from bigdecimal.

TomNaessens avatar TomNaessens commented on July 30, 2024

Same happens for NaN, looks like another side effect of the same case:

irb(main):029:0> BigDecimal('NaN').round(1)
=> NaN

irb(main):031:0> BigDecimal('NaN').round(0)
(irb):31:in `round`: Computation results to 'NaN'(Not a Number) (FloatDomainError)

irb(main):032:0> BigDecimal('NaN').round(-1)
(irb):32:in `round`: Computation results to 'NaN'(Not a Number) (FloatDomainError)

from bigdecimal.

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.