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 6331087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:59:28+00:00 2026-05-24T17:59:28+00:00

I suspect this has been asked before, but can’t seem to find a question

  • 0

I suspect this has been asked before, but can’t seem to find a question that matches…

I’m using Scala, but I’m pretty sure this is just a Java problem… input values are doubles

println(28.0 / 5.6) 
println(28.0 % 5.6)

The result of these lines is

5.0 
1.7763568394002505E-15 

Which means that Java is performing the division correctly, but for some reason getting the modulo wrong, since the modulo should be 0 for any division problem that resolves to a whole number…

Is there a workaround for this?

Thanks!

  • 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-24T17:59:30+00:00Added an answer on May 24, 2026 at 5:59 pm

    The 5.0 just shows that the precise result as Java understands it is closer to 5.0 than it is to any other double. That doesn’t mean the precise result of the operation is exactly 5.

    Now when you ask for the modulus, you’re able to down to a much finer level of detail, because the result isn’t pinned to having the “5” part.

    That’s not a great explanation, but imagine you had a decimal floating point type with 4 digits of precision. What’s the result of 1000 / 99.99 and 1000 % 99.99?

    Well, the real result starts with 10.001001 – so you have to round that to 10.00. However, the remainder is 0.10, which you can express. So again, it looks like the division gives you a whole number, but it doesn’t quite.

    With that in mind, bear in mind that your literal of 5.6 is actually 5.5999999999999996447286321199499070644378662109375. Now clearly 28.0 (which *can) be represented exactly divided by that number isn’t exactly 5.

    EDIT: Now if you perform the result with decimal floating point arithmetic using BigDecimal, the value really is exactly 5.6, and there are no problems:

    import java.math.BigDecimal;
    
    public class Test {
        public static void main(String[] args) {
            BigDecimal x = new BigDecimal("28.0");
            BigDecimal y = new BigDecimal("5.6");
    
            BigDecimal div = x.divide(y);
            BigDecimal rem = x.remainder(y);
    
            System.out.println(div); // Prints 5
            System.out.println(rem); // Prints 0.0
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I suspect that this has been asked before, but I have no idea what
I suspect this question has been asked before, but it's not easy to Google
I suspect this has alerady been asked, but I could not find a way
I suspect this has already been answered somewhere, but I can't find it, so...
I searched here for the answer. I'm sorry if this has been asked before
Sorry I know similar question have been asked many times before but like most
This has been bugging me for the past several hours and I can't seem
I am quite sure this question has already been asked several times and I
This has been bugging me for years, but I've just been ignoring it, like
I'm almost positive this topic has been addressed somewhere because my question feels so

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.