Giter Club home page Giter Club logo

Comments (3)

MarcelVersteeg avatar MarcelVersteeg commented on May 31, 2024 1

I have been debugging my test script, first without the use Locale::CLDR (which succeeds) and then with the use Locale::CLDR (which fails). I see some strange things, that I can't quite explain, but there is some difference.

Without use Locale::CLDR
On line 260 of the file CLDR/Number/Role/Format.pm, you call Math::BigFloat->($num). As $num is the integer value 42, Math::BigFloat creates a Math::BigInt and stores that one internally. The call to ffround on this new object will actually result in a call to function Math::BigFloat::bfround (note the difference in the first character of the function name!) and this call will actually succeed.
The module Math::BigFloat also has a module variable called $downgrade. When Locale::CLDR is not loaded, this variable has the value undef.

With use Locale::CLDR
On line 260 of the file CLDR/Number/Role/Format.pm, you call Math::BigFloat->($num). As $num is the integer value 42, Math::BigFloat creates a Math::BigInt and stores that one internally. The call to ffround on this new object in this case does not result in a call to function Math::BigFloat::bfround (note the difference in the first character of the function name!), but indeed tries to call ffround on the Math::BigInt that was stored internally and that function does not exist.
When the module Locale::CLDR is loaded, the module Math::BigFloat variable $downgrade has the value Math::BigInt. This probably makes the difference in handling the ffround.

Now examining the code of Math::BigFloat further, there is only one location where the modules variable $downgrade is set, and that is in its import function when one of the arguments is 'downgrade' (which does not seem to be documented on cpan BTW). The fact that $downgrade is set when using Locale::CDLR is because the module Locale::CLDR::NumberFormatter uses bignum.

My proposal would be to change the following code in the function CLDR::Number::Role::Format::_format_number (lines 258 to 264)

        else {
            # round half to even
            $rounded = Math::BigFloat->new($num)->ffround(
                -$self->maximum_fraction_digits,
                'even'
            )->babs->bstr;
        }

to

        else {
            # round half to even
            my $mbf = Math::BigFloat->new($num);
            if (ref $mbf eq 'Math::BigInt')     # Needed in case another module uses Math::BigFloat with the 'downgrade' option
            {
                $rounded = $mbf->bfround(
                    -$self->maximum_fraction_digits,
                    'even'
                );
            }
            else
            {
                $rounded = $mbf->ffround(
                    -$self->maximum_fraction_digits,
                    'even'
                );
            }
            $rounded = $rounded->babs->bstr;
        }

Would it be possible to make this change in the repository and create a new release of CLDR::Number in metacpan? Of course I can locally patch it, but that would not make it available to other users of this module.

from cldr-number-pm5.

patch avatar patch commented on May 31, 2024

Hi Marcel, thanks for reporting this. I’m not sure why Locale::CLDR causes CLDR::Number to break. I do wonder why ffround is associated with Math::BigInt in your error message, as it’s actually part of Math::BigFloat which I’m using. I’m not able to help troubleshoot at this time but would happily accept any fixes. I’m not familiar with any modern CLDR-related modules in CPAN, although I can show you how to easily accomplish this in JavaScript in case your project uses frontend code.

from cldr-number-pm5.

MarcelVersteeg avatar MarcelVersteeg commented on May 31, 2024

I will try to find out the actual cause of this (will also contact the developer of Locale::CLDR). I will let you know the outcome and if I am able to make a fix for it.

from cldr-number-pm5.

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.