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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:23:36+00:00 2026-05-26T20:23:36+00:00

Recently I discovered that C#’s operator % is applicable to double. Tried some things

  • 0

Recently I discovered that C#’s operator % is applicable to double. Tried some things out, and after all came up with this test:

class Program
{
    static void test(double a, double b)
    {
        if (a % b != a - b * Math.Truncate(a / b))
        {
            Console.WriteLine(a + ", " + b);
        }
    }
    static void Main(string[] args)
    {
        test(2.5, 7);
        test(-6.7, -3);
        test(8.7, 4);
        //...
    }
}

Everything in this test works.
Is a % b always equivalent to a - b*Math.Round(a/b)? If not, please explain to me how this operator really works.

EDIT: Answering to James L, I understand that this is a modulo operator and everything. I’m curious only about how it works with double, integers I understand.

  • 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-26T20:23:37+00:00Added an answer on May 26, 2026 at 8:23 pm

    The modulus operator works on floating point values in the same way as it does for integers. So consider a simple example:

    4.5 % 2.1
    

    Now, 4.5/2.1 is approximately equal to 2.142857

    So, the integer part of the division is 2. Subtract 2*2.1 from 4.5 and you have the remainer, 0.3.

    Of course, this process is subject to floating point representability issues so beware – you may see unexpected results. For example, see this question asked here on Stack Overflow: Floating Point Arithmetic – Modulo Operator on Double Type


    Is a % b always equivalent to a – b*Math.Round(a/b)?

    No it is not. Here is a simple counter example:

    static double f(double a, double b)
    {
        return a - b * Math.Round(a / b);
    }
    
    static void Main(string[] args)
    {
        Console.WriteLine(1.9 % 1.0);
        Console.WriteLine(f(1.9, 1.0));
        Console.ReadLine();
    }
    

    As to the precise details of how the modulus operator is specified you need to refer to the C# specification – earlNameless’s answer gives you a link to that.

    It is my understanding that a % b is essentially equivalent, modulo floating point precision, to a - b*Math.Truncate(a/b).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently discovered that 2 == [2] in JavaScript. As it turns out, this
I recently discovered that in C++ you can overload the function call operator, in
I have recently discovered that I am affected by this bug http://www.mail-archive.com/mono-bugs@lists.ximian.com/msg71515.html Well, at
I recently discovered that some functionality has been changed drastically in the DataGrid since
We’ve recently discovered that WCF does not support timing out operations on the service
Recently, I discovered that Internet Explorer flat-out doesn't support certain selectors in jQuery (see
We recently discovered that the Google Maps API does not play nicely with SSL.
I recently discovered that when you have a page set to session readonly and
I recently discovered that I can use lambdas to create simple event handlers. I
I recently discovered that a sql query that was running fine earlier is now

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.