Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 790349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T21:40:05+00:00 2026-05-14T21:40:05+00:00

In the spirit of using existing, tested and stable libraries of code, I started

  • 0

In the spirit of using existing, tested and stable libraries of code, I started using the Apache-Commons-Math library and its BigFraction class to perform some rational calculations for an Android app I’m writing called RationalCalc.

It works great for every task that I have thrown at it, except for one nagging problem. When dividing certain BigFraction values, I am getting incorrect results.

If I create a BigFraction with the inverse of the divisor and multiply instead, I get the same incorrect answer but perhaps that is what the library is doing internally anyway.

Does anyone know what I am doing wrong?

The division works correctly with a BigFraction of 2.5 but not 2.51, 2.49, etc…

[UPDATE]

This was indeed a bug in the apache-commons-math 2.0 libraries. The bug is fixed in v.2.1.

It is now listed in the Fixed Issues section of the bug tracker:

When multiplying two BigFraction objects with numerators larger than will fit in an java-primitive int the result of BigFraction.ZERO is incorrectly returned..

Thanks to @BartK for attempting to reproduce the issue and setting me on the right track.

[/UPDATE]

// *** incorrect! ***
BigFraction one = new BigFraction(1.524);
//one: 1715871458028159 / 1125899906842624

BigFraction two = new BigFraction(2.51);
//two: 1413004383087493 / 562949953421312

BigFraction three = one.divide(two);
//three: 0

Log.i("solve", three.toString());
//should be 0.607171315  ??
//returns 0


// *** correct! ****
BigFraction four = new BigFraction(1.524);
//four: 1715871458028159 / 1125899906842624

BigFraction five = new BigFraction(2.5);
//five: 5 / 2

BigFraction six = four.divide(five);
//six: 1715871458028159 / 2814749767106560

Log.i("solve", six.toString());
//should be 0.6096  ??
//returns 0.6096
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T21:40:05+00:00Added an answer on May 14, 2026 at 9:40 pm

    Providing double‘s in the constructors lead to round-off errors. Using exact numerators and denominators will result in the expected outcome:

    public class CommonsMathTest {
    
        public static void main(String[] args) {
    
            BigFraction one = new BigFraction(1524, 1000);
            System.out.println("one   = " + one);
    
            BigFraction two = new BigFraction(251, 100);
            System.out.println("two   = " + two);
    
            BigFraction three = one.divide(two);
            System.out.println("three = " + three);
    
            BigFraction four = new BigFraction(1524, 1000);
            System.out.println("four  = " + four);
    
            BigFraction five = new BigFraction(5, 2);
            System.out.println("five  = " + five);
    
            BigFraction six = four.divide(five);
            System.out.println("six   = " + six + " = " + six.bigDecimalValue());
        }
    }
    

    produces:

    one   = 381 / 250
    two   = 251 / 100
    three = 762 / 1255
    four  = 381 / 250
    five  = 5 / 2
    six   = 381 / 625 = 0.6096
    

    EDIT

    By the way, I could not reproduce your output. Using Commons-Math 2.1, the following:

    BigFraction one = new BigFraction(1.524);
    BigFraction two = new BigFraction(2.51);
    BigFraction three = one.divide(two);
    System.out.println(three.toString() + " = " +three.doubleValue());
    

    does not produce 0 as you said, but prints:

    1715871458028159 / 2826008766174986 = 0.6071713147410359
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In the spirit of re-using code, I'm trying to create a few library projects.
I started using spirit yesterday. I have to write a parser for a small
I am experiencing a crash while using the Boost.Spirit and Boost.Thread libraries in my
I am using Boost Spirit parser, and as the parser is parsing, semantic actions
I'm currently trying to port a fair amount of existing synchronous code to WinRT.
Continuing with the spirit of using the Stack Exchange Data Explorer to learn SQL,
I am trying to use the Spirit library for the first time. I am
I'm trying to integrate CUDA to an existing aplication wich uses boost::spirit. Isolating the
Using Boost.Spirit, is it possible to deliver a terminal token to multiple actions and
I want to parse a file into an AST using Boost Spirit. The root

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.