473,395 Members | 1,987 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,395 software developers and data experts.

Error in decimal divide in "?:" operator

I have a web service written in C# Visual Studio 2005 that calculates splits
between two or more timekeepers in a transaction. To set this up I have
several decimal data type variables from one particular timekeeper:

timerRow["diff_time"] = 4.5M
tkRate = 130M
tkAmount = 585M (timerRow["diff_time"] * tkRate)
totalAmount = 1605M (sum of all tkAmounts in the transaction)

I execute the following line of code:

percentOfTransaction = (totalAmount == 0.00M ? 0.00M : tkAmount /
totalAmount);

and get 0.4382022471910112359550561798 however the actual answer should be
0.36448598130841121495327102803738.

If I replace the code with:

percentOfTotal = 0.00M;

if (totalAmount != 0.00M)
{
percentOfTotal = tkAmount / totalAmount;
}

I get the correct answer. The error does not occur for every transaction as
many of the calcuations come out correct. This is the sixth transaction in a
batch of eight and the only one that is wrong.

The other curious thing is that, using the command window, if I type

? tkAmount / totalAmount

it responds with the correct answer and

? percentOfTransaction

yields the erroneous result indicating that the variables are set properly.

Has anyone seen this behavior before as I use this construct a great deal in
checking for divide by zero?

--
Regards,

JayAchTee
Mar 7 '07 #1
10 2953
The first thing I'd do is put in some parentheses and see if the operator
grouping is what I think it is.

"JayAchTee" <Ja*******@discussions.microsoft.comwrote in message
news:BD**********************************@microsof t.com...
>I have a web service written in C# Visual Studio 2005 that calculates
splits
between two or more timekeepers in a transaction. To set this up I have
several decimal data type variables from one particular timekeeper:

timerRow["diff_time"] = 4.5M
tkRate = 130M
tkAmount = 585M (timerRow["diff_time"] * tkRate)
totalAmount = 1605M (sum of all tkAmounts in the transaction)

I execute the following line of code:

percentOfTransaction = (totalAmount == 0.00M ? 0.00M : tkAmount /
totalAmount);

and get 0.4382022471910112359550561798 however the actual answer should be
0.36448598130841121495327102803738.

If I replace the code with:

percentOfTotal = 0.00M;

if (totalAmount != 0.00M)
{
percentOfTotal = tkAmount / totalAmount;
}

I get the correct answer. The error does not occur for every transaction
as
many of the calcuations come out correct. This is the sixth transaction
in a
batch of eight and the only one that is wrong.

The other curious thing is that, using the command window, if I type

? tkAmount / totalAmount

it responds with the correct answer and

? percentOfTransaction

yields the erroneous result indicating that the variables are set
properly.

Has anyone seen this behavior before as I use this construct a great deal
in
checking for divide by zero?

--
Regards,

JayAchTee

Mar 7 '07 #2
JayAchTee wrote:
I have a web service written in C# Visual Studio 2005 that calculates splits
between two or more timekeepers in a transaction. To set this up I have
several decimal data type variables from one particular timekeeper:
Using your code (roughly), I get:

~$ cat prog.cs
using System;

class App
{
static void Main()
{
decimal diff_time = 4.5M;
decimal rate = 130M;
decimal amount = diff_time * rate;
decimal totalAmount = 1605M;
decimal percent = (totalAmount == 0M ? 0M : amount /
totalAmount);
Console.WriteLine(percent);
}
}

~$ ./prog
0.364485981308411214953271028

Can you reproduce your behaviour in a simple, complete program like the
one above?

-- Barry

--
http://barrkel.blogspot.com/
Mar 7 '07 #3
I would think it was grouping except for the fact that it works seven out of
eight times with sixteen passes through the code (8 transaction * 2
timekeepers)!
--
Regards,

JayAchTee
"Michael A. Covington" wrote:
The first thing I'd do is put in some parentheses and see if the operator
grouping is what I think it is.

"JayAchTee" <Ja*******@discussions.microsoft.comwrote in message
news:BD**********************************@microsof t.com...
I have a web service written in C# Visual Studio 2005 that calculates
splits
between two or more timekeepers in a transaction. To set this up I have
several decimal data type variables from one particular timekeeper:

timerRow["diff_time"] = 4.5M
tkRate = 130M
tkAmount = 585M (timerRow["diff_time"] * tkRate)
totalAmount = 1605M (sum of all tkAmounts in the transaction)

I execute the following line of code:

percentOfTransaction = (totalAmount == 0.00M ? 0.00M : tkAmount /
totalAmount);

and get 0.4382022471910112359550561798 however the actual answer should be
0.36448598130841121495327102803738.

If I replace the code with:

percentOfTotal = 0.00M;

if (totalAmount != 0.00M)
{
percentOfTotal = tkAmount / totalAmount;
}

I get the correct answer. The error does not occur for every transaction
as
many of the calcuations come out correct. This is the sixth transaction
in a
batch of eight and the only one that is wrong.

The other curious thing is that, using the command window, if I type

? tkAmount / totalAmount

it responds with the correct answer and

? percentOfTransaction

yields the erroneous result indicating that the variables are set
properly.

Has anyone seen this behavior before as I use this construct a great deal
in
checking for divide by zero?

--
Regards,

JayAchTee


Mar 7 '07 #4
I have not had any luck at duplicating the problem by simplifying the
application. The results are as expected. This one has me and my colleagues
baffeled.
--
Regards,

JayAchTee
"Barry Kelly" wrote:
JayAchTee wrote:
I have a web service written in C# Visual Studio 2005 that calculates splits
between two or more timekeepers in a transaction. To set this up I have
several decimal data type variables from one particular timekeeper:

Using your code (roughly), I get:

~$ cat prog.cs
using System;

class App
{
static void Main()
{
decimal diff_time = 4.5M;
decimal rate = 130M;
decimal amount = diff_time * rate;
decimal totalAmount = 1605M;
decimal percent = (totalAmount == 0M ? 0M : amount /
totalAmount);
Console.WriteLine(percent);
}
}

~$ ./prog
0.364485981308411214953271028

Can you reproduce your behaviour in a simple, complete program like the
one above?

-- Barry

--
http://barrkel.blogspot.com/
Mar 7 '07 #5
JayAchTee <Ja*******@discussions.microsoft.comwrote:
I have a web service written in C# Visual Studio 2005 that calculates splits
between two or more timekeepers in a transaction. To set this up I have
several decimal data type variables from one particular timekeeper:

timerRow["diff_time"] = 4.5M
tkRate = 130M
tkAmount = 585M (timerRow["diff_time"] * tkRate)
totalAmount = 1605M (sum of all tkAmounts in the transaction)

I execute the following line of code:

percentOfTransaction = (totalAmount == 0.00M ? 0.00M : tkAmount /
totalAmount);

and get 0.4382022471910112359550561798 however the actual answer should be
0.36448598130841121495327102803738.
Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 7 '07 #6
Unfortunately the simplified code does not reproduce the problem. All I have
left of the issue is a screen shot of the debugging session with the watch
variables and command line window output.
--
Regards,

JayAchTee
"Jon Skeet [C# MVP]" wrote:
JayAchTee <Ja*******@discussions.microsoft.comwrote:
I have a web service written in C# Visual Studio 2005 that calculates splits
between two or more timekeepers in a transaction. To set this up I have
several decimal data type variables from one particular timekeeper:

timerRow["diff_time"] = 4.5M
tkRate = 130M
tkAmount = 585M (timerRow["diff_time"] * tkRate)
totalAmount = 1605M (sum of all tkAmounts in the transaction)

I execute the following line of code:

percentOfTransaction = (totalAmount == 0.00M ? 0.00M : tkAmount /
totalAmount);

and get 0.4382022471910112359550561798 however the actual answer should be
0.36448598130841121495327102803738.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 7 '07 #7
You mentioned evaluating some of the sub-expressions in the debugger? What
if you try evaluate the whole ternary (?:) expression in the debugger - do
you get the same result? Note: not sure if it can do this or not.

"JayAchTee" <Ja*******@discussions.microsoft.comwrote in message
news:33**********************************@microsof t.com...
>I have not had any luck at duplicating the problem by simplifying the
application. The results are as expected. This one has me and my
colleagues
baffeled.
--
Regards,

JayAchTee
"Barry Kelly" wrote:
>JayAchTee wrote:
I have a web service written in C# Visual Studio 2005 that calculates
splits
between two or more timekeepers in a transaction. To set this up I
have
several decimal data type variables from one particular timekeeper:

Using your code (roughly), I get:

~$ cat prog.cs
using System;

class App
{
static void Main()
{
decimal diff_time = 4.5M;
decimal rate = 130M;
decimal amount = diff_time * rate;
decimal totalAmount = 1605M;
decimal percent = (totalAmount == 0M ? 0M : amount /
totalAmount);
Console.WriteLine(percent);
}
}

~$ ./prog
0.364485981308411214953271028

Can you reproduce your behaviour in a simple, complete program like the
one above?

-- Barry

--
http://barrkel.blogspot.com/

Mar 7 '07 #8
Kevin, I did not try that... but I can tomorrow. I have set up the
application to fault when the percentages don't add up to 100% and have also
added a check feature using the ?: expression and simple if-then logic. I'll
let you know.

--
Regards,

JayAchTee
"Kevin Frey" wrote:
You mentioned evaluating some of the sub-expressions in the debugger? What
if you try evaluate the whole ternary (?:) expression in the debugger - do
you get the same result? Note: not sure if it can do this or not.

"JayAchTee" <Ja*******@discussions.microsoft.comwrote in message
news:33**********************************@microsof t.com...
I have not had any luck at duplicating the problem by simplifying the
application. The results are as expected. This one has me and my
colleagues
baffeled.
--
Regards,

JayAchTee
"Barry Kelly" wrote:
JayAchTee wrote:

I have a web service written in C# Visual Studio 2005 that calculates
splits
between two or more timekeepers in a transaction. To set this up I
have
several decimal data type variables from one particular timekeeper:

Using your code (roughly), I get:

~$ cat prog.cs
using System;

class App
{
static void Main()
{
decimal diff_time = 4.5M;
decimal rate = 130M;
decimal amount = diff_time * rate;
decimal totalAmount = 1605M;
decimal percent = (totalAmount == 0M ? 0M : amount /
totalAmount);
Console.WriteLine(percent);
}
}

~$ ./prog
0.364485981308411214953271028

Can you reproduce your behaviour in a simple, complete program like the
one above?

-- Barry

--
http://barrkel.blogspot.com/


Mar 8 '07 #9
JayAchTee <Ja*******@discussions.microsoft.comwrote:
Unfortunately the simplified code does not reproduce the problem. All I have
left of the issue is a screen shot of the debugging session with the watch
variables and command line window output.
Sounds like it's more likely to be a debugger issue than anything else,
to be honest.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 8 '07 #10
That's what I would have thought except for the incorrect numbers in the time
and billing system! Just a fluke... no big deal. Right!
--
Regards,

JayAchTee
"Jon Skeet [C# MVP]" wrote:
JayAchTee <Ja*******@discussions.microsoft.comwrote:
Unfortunately the simplified code does not reproduce the problem. All I have
left of the issue is a screen shot of the debugging session with the watch
variables and command line window output.

Sounds like it's more likely to be a debugger issue than anything else,
to be honest.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 8 '07 #11

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

Similar topics

687
by: cody | last post by:
no this is no trollposting and please don't get it wrong but iam very curious why people still use C instead of other languages especially C++. i heard people say C++ is slower than C but i can't...
2
by: Steve Summit | last post by:
-----BEGIN PGP SIGNED MESSAGE----- It's often explained that the reason for some of the imprecision in C's definition is so that C can be implemented on different kinds of machines -- say, those...
12
by: ThunderMusic | last post by:
Hi, We have a part of our application that deals with millions of records and do some processing of them. We've achieved a pretty good performance gain by developping a custom DateTime.ToString...
3
by: jer006 | last post by:
Hi I am writing a select statement that has an arithmetic function inside a case statement that uses logic to decide whether to divide or multiply and when I run the arithmetic statements outside...
1
by: Vitaliy | last post by:
Hi I got this wired exception periodically (Python 2.5, Django based application) what does it mean ?
2
by: clintonb | last post by:
Victor said: The double value that I'm trying to convert to GCSMoney (which is implemented as cents) was produced by multiplying a dollar amount by an interest rate to get interest. double...
13
by: =?Utf-8?B?RXRoYW4gU3RyYXVzcw==?= | last post by:
Hi, Why does Math.Sqrt() only accept a double as a parameter? I would think it would be just as happy with a decimal (or int, or float, or ....). I can easily convert back and forth, but I am...
2
by: rijaalu | last post by:
I am designing a matrix class that performs addition, multicpication, substraction and division. When ever i complie the code it shows an error. include <iostream> using namespace std; class...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.