473,399 Members | 2,858 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

inaccurate modulo function?

I played a little bit with modulo cause I wanted to implement a small
parser for functions as I stumbled over a calculation error. First I
thought the reason might be a type conversion I did before, but see
this small example:

using System;
namespace modulo_test
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
Console.WriteLine("(5%2.2) = {0}\n", 5%2.2);
for (double i=0.1; i<1; i=i+0.1)
{
double calc1 = (5%2.2)-i;
double calc2 = 0.6-i;
Console.WriteLine("(5%2.2)-{0} = {1} \t 0.6-{0} = {2}",
i, calc1, calc2);
}
}
}
}

---------
Output:

(5%2.2) = 0.6

(5%2.2)-0.1 = 0.5 0.6-0.1 = 0.5
(5%2.2)-0.2 = 0.4 0.6-0.2 = 0.4
(5%2.2)-0.3 = 0.3 0.6-0.3 = 0.3
(5%2.2)-0.4 = 0.2 0.6-0.4 = 0.2
(5%2.2)-0.5 = 0.0999999999999996 0.6-0.5 = 0.1
(5%2.2)-0.6 = -3.33066907387547E-16 0.6-0.6 = 0
(5%2.2)-0.7 = -0.1 0.6-0.7 = -0.1
(5%2.2)-0.8 = -0.2 0.6-0.8 = -0.2
(5%2.2)-0.9 = -0.3 0.6-0.9 = -0.3
(5%2.2)-1 = -0.4 0.6-1 = -0.4

---------

Obviously 5 modulo 2.2 is exactly 0.6, but if I calculate further with
it, you see, that the result of the modulo function is not exact.
Nov 16 '05 #1
2 5334
Alexander,

It's not that the modulo function is inaccurate, but rather the double
type can't accurately represent floating point values. If you need this
kind of precision, then you should use the Decimal type. The following code
will give you what you want:

using System;

namespace modulo_test
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
Console.WriteLine("(5%2.2) = {0}\n", 5 % 2.2m);
for (Decimal i = 0.1m; i < 1; i = i + 0.1m)
{
Decimal calc1 = (5 % 2.2m) - i;
Decimal calc2 = 0.6m - i;
Console.WriteLine("(5%2.2)-{0} = {1} \t 0.6-{0} = {2}",
i, calc1, calc2);
}
}
}
}

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Alexander" <jo*******@gmx.de> wrote in message
news:84**************************@posting.google.c om...
I played a little bit with modulo cause I wanted to implement a small
parser for functions as I stumbled over a calculation error. First I
thought the reason might be a type conversion I did before, but see
this small example:

using System;
namespace modulo_test
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
Console.WriteLine("(5%2.2) = {0}\n", 5%2.2);
for (double i=0.1; i<1; i=i+0.1)
{
double calc1 = (5%2.2)-i;
double calc2 = 0.6-i;
Console.WriteLine("(5%2.2)-{0} = {1} \t 0.6-{0} = {2}",
i, calc1, calc2);
}
}
}
}

---------
Output:

(5%2.2) = 0.6

(5%2.2)-0.1 = 0.5 0.6-0.1 = 0.5
(5%2.2)-0.2 = 0.4 0.6-0.2 = 0.4
(5%2.2)-0.3 = 0.3 0.6-0.3 = 0.3
(5%2.2)-0.4 = 0.2 0.6-0.4 = 0.2
(5%2.2)-0.5 = 0.0999999999999996 0.6-0.5 = 0.1
(5%2.2)-0.6 = -3.33066907387547E-16 0.6-0.6 = 0
(5%2.2)-0.7 = -0.1 0.6-0.7 = -0.1
(5%2.2)-0.8 = -0.2 0.6-0.8 = -0.2
(5%2.2)-0.9 = -0.3 0.6-0.9 = -0.3
(5%2.2)-1 = -0.4 0.6-1 = -0.4

---------

Obviously 5 modulo 2.2 is exactly 0.6, but if I calculate further with
it, you see, that the result of the modulo function is not exact.

Nov 16 '05 #2
Alexander <jo*******@gmx.de> wrote:
I played a little bit with modulo cause I wanted to implement a small
parser for functions as I stumbled over a calculation error. First I
thought the reason might be a type conversion I did before, but see
this small example:


<snip>

See http://www.pobox.com/~skeet/csharp/floatingpoint.html

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Griff | last post by:
Test program: =============================================================== t = 5.8 interval = 2.0 while t < 6.1: print "%s mod %s = %s " % (t, interval, t % interval ) t += 0.1...
2
by: Siemel Naran | last post by:
About inaccurate floating point and DBL_MAX. double x = 2; double y = 1 + 1; assert(x == y); Because of inaccurate floating point representation, x and y may not be equal (ie. they may...
4
by: Christine | last post by:
I've implemented a countdown timer for a tutorial web page that should give the user 45 minutes to complete the test, only to find that the timer is slowly 'losing' time. On average, it actually...
5
by: silentlights | last post by:
Hi, Is there a possibiliy to improve division or Modulo operations in the following, tmp1 = 123; tmp2 = 123; frame = ((char)((tmp1/100)+48)); // Division tmp1 = (tmp2...
10
by: john blackburn | last post by:
Hi, I am trying to get an extremely simple character string encryption function to work using modulo 10 arithmetic. void ccencode(UNSIGNED8* pattern) { UNSIGNED8 key = CCKEY; UNSIGNED8...
11
by: Ben Blank | last post by:
I have a loop which iterates over an array in a particular order: for (j = 0; j < 16; j++) T = ...; The loop proceeds normally for j = 0 through j = 4, but gives an IndexOutOfRangeException at...
12
by: Chadwick Boggs | last post by:
I need to perform modulo operations on extremely large numbers. The % operator is giving me number out of range errors and the mod(x, y) function simply seems to return the wrong results. Also,...
2
by: Jana | last post by:
Using Access 97. Background: I have a main report called rptTrustHeader with a subreport rptTrustDetails in the Details section of the main report. The main report is grouped by MasterClientID. ...
17
by: Dawoodoz | last post by:
-24 mod 7 = -3 The mathematical modulo can't return a value less than 0 according to the professors at my University but the function in Visual Basic 6 does. Is there a fast way around this...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.