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

Why does this math operation overflow?

Why does this overflow?

long testmath = (898 * 360 + 30) * 24 * 60 * 60 * 1000 / 25;

but if I do it this way it works..

long testmath1 = (898 * 360 + 30)
long testmath2 = testmath1 * 24 * 60 * 60 * 1000 / 25;

How can I get it so that it works properly in the first definition..?

Thanks.
Nov 15 '05 #1
3 1554
I believe the reason it works is that all of the operands are evaluated
as signed 32-bit integers, causing the overflow. When you assign the value
to testmath1, it is a long, which causes the second expression to have all
of its operands converted to 64 bit integers. In order to get this to work
in one line, this should work:

long testmath = ((long) (898 * 360 + 30)) * 24 * 60 * 60 * 1000 / 25;

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Unknown" <Un*****@Unknown.COM> wrote in message
news:Xn*******************************@140.99.99.1 30...
Why does this overflow?

long testmath = (898 * 360 + 30) * 24 * 60 * 60 * 1000 / 25;

but if I do it this way it works..

long testmath1 = (898 * 360 + 30)
long testmath2 = testmath1 * 24 * 60 * 60 * 1000 / 25;

How can I get it so that it works properly in the first definition..?

Thanks.

Nov 15 '05 #2
Unknown wrote:
Why does this overflow?

long testmath = (898 * 360 + 30) * 24 * 60 * 60 * 1000 / 25;

but if I do it this way it works..

long testmath1 = (898 * 360 + 30)
long testmath2 = testmath1 * 24 * 60 * 60 * 1000 / 25;

How can I get it so that it works properly in the first definition..?

Thanks.


The first expression overflows because all of the components of the
expression are of type Int32 (the assignment destination is not used to
determine the expression type).

In your second instance, testmath1 has type Int64, so the expression is
also Int64.

You can get the first expression to work by casting one or more of the
operands to Int64 or long:

long testmath = ((long) (898 * 360 + 30)) * 24 * 60 * 60 * 1000 / 25;
--
mikeb
Nov 15 '05 #3
100
Hi Unknown,
Just let at least one of the constant have to *L* sufix.
long testmath = (898L * 360 + 30) * 24 * 60 * 60 * 1000 / 25;
This will do the trick.

HTH
B\rgds
100

"Unknown" <Un*****@Unknown.COM> wrote in message
news:Xn*******************************@140.99.99.1 30...
Why does this overflow?

long testmath = (898 * 360 + 30) * 24 * 60 * 60 * 1000 / 25;

but if I do it this way it works..

long testmath1 = (898 * 360 + 30)
long testmath2 = testmath1 * 24 * 60 * 60 * 1000 / 25;

How can I get it so that it works properly in the first definition..?

Thanks.

Nov 15 '05 #4

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

Similar topics

10
by: Fredrik Celin | last post by:
I use IE 6.0 and does some calculations in a javascript. If I run this code: alert(1.4 - 0.5); i get the result 0.8999999999 and not 0.9 Does anybody know why? Is this a bug in IE or...
58
by: Larry David | last post by:
Ok, first of all, let's get the obvious stuff out of the way. I'm an idiot. So please indulge me for a moment. Consider it an act of "community service".... What does "64bit" mean to your friendly...
4
by: glenn | last post by:
I have a COM Server I've written in C#. I have a client app I've written in Delphi that is calling the C# COM Server. However, one of the functions in the COM Server creates a form and during the...
4
by: Simon Devlin | last post by:
Hi folks, I've been bashing my head against this all afternoon and am now totally baffled. Given this (a simple routine to turn a ip address string into an decimal) <snip> Dim Parts(3) as...
13
by: david ullua | last post by:
Hi, In Expand.c of BSD system, I met the following codes, the expression (column & 07) if used to compare variable column and 7, if column<=7, it returns true, else false. It use (column & 07)...
1
by: thebigsquid | last post by:
hi, its me again, so this software obviously has serious problems. now i'm getting this error message when i try to access any of it! any ideas much appreciated thanks the big squid An...
7
by: pcauchon | last post by:
When I try to run this code on my machine (iMac with MacOS 10.5), I get very strange results. I am using this compiler: i686-apple-darwin9-gcc-4.0.1 (GCC) 4.0.1 (Apple Inc. build 5465) The first...
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: bips2008 | last post by:
The code seems to work fine in other browser but in IE it throws this error. This is very urgent for me and any help would be greatly appreciated For your convienence i have posted the code for the...
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...
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
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
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
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
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...

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.