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

explict conversion from double to int - regarding overflow handling

I am a bit confused as to why conversion from float/double to int is
handled in c# in the way that it is. It differs from my implementation
of C and C++ in a suprising way (to me).

Here is a code snippet to reproduce my suprise:

// c#
int max = int.MaxValue;
Console.Out.WriteLine("max = " + max);
Console.Out.WriteLine("(int)(max + 1.0) = " + (int)(max + 1.0));
Console.Out.WriteLine("(int)(max + 2.0) = " + (int)(max + 2.0));
Console.Out.WriteLine("(int)(max + 1) = " + (int)(max + 1));
Console.Out.WriteLine("(int)(max + 2) = " + (int)(max + 2));

output:
1. max = 2147483647
2. (int)(max + 1.0) = -2147483648
3. (int)(max + 2.0) = -2147483648
4. (int)(max + 1) = -2147483648
5. (int)(max + 2) = -2147483647

// c
int max = 2147483647;

printf("max = %d\n", max);
printf("(int)(max + 1.0) = %d\n", (int)(max + 1.0));
printf("(int)(max + 2.0) = %d\n", (int)(max + 2.0));
printf("(int)(max + 1) = %d\n", (int)(max + 1));
printf("(int)(max + 2) = %d\n", (int)(max + 2));

1. max = 2147483647
2. (int)(max + 1.0) = -2147483648
3. (int)(max + 2.0) = -2147483647
4. (int)(max + 1) = -2147483648
5. (int)(max + 2) = -2147483647

notice how line #3 yield different results!

So I read the c# language spec, and it appears that it is legal to do
what I am seeing, BUT I think it is counter to what most programmers
would expect. I also read the c++ language spec, and again the
behavior I expected is not guarenteed! Wow! Is this suprising to any
of you guys?

Felix
Nov 16 '05 #1
1 1893
Hello,

I'm dealing with the same problem man. (See the vjsharp newsgroup for the
thread about my exact problem). But anyway, I got linked to:

http://www.yoda.arachsys.com/csharp/floatingpoint.html
http://www.yoda.arachsys.com/csharp/decimal.html

And those seem to explain why this kind of jazz happens in .NET.

- Nick

Felix wrote:
I am a bit confused as to why conversion from float/double to int is
handled in c# in the way that it is. It differs from my implementation
of C and C++ in a suprising way (to me).

Here is a code snippet to reproduce my suprise:

// c#
int max = int.MaxValue;
Console.Out.WriteLine("max = " + max);
Console.Out.WriteLine("(int)(max + 1.0) = " + (int)(max + 1.0));
Console.Out.WriteLine("(int)(max + 2.0) = " + (int)(max + 2.0));
Console.Out.WriteLine("(int)(max + 1) = " + (int)(max + 1));
Console.Out.WriteLine("(int)(max + 2) = " + (int)(max + 2));

output:
1. max = 2147483647
2. (int)(max + 1.0) = -2147483648
3. (int)(max + 2.0) = -2147483648
4. (int)(max + 1) = -2147483648
5. (int)(max + 2) = -2147483647

// c
int max = 2147483647;

printf("max = %d\n", max);
printf("(int)(max + 1.0) = %d\n", (int)(max + 1.0));
printf("(int)(max + 2.0) = %d\n", (int)(max + 2.0));
printf("(int)(max + 1) = %d\n", (int)(max + 1));
printf("(int)(max + 2) = %d\n", (int)(max + 2));

1. max = 2147483647
2. (int)(max + 1.0) = -2147483648
3. (int)(max + 2.0) = -2147483647
4. (int)(max + 1) = -2147483648
5. (int)(max + 2) = -2147483647

notice how line #3 yield different results!

So I read the c# language spec, and it appears that it is legal to do
what I am seeing, BUT I think it is counter to what most programmers
would expect. I also read the c++ language spec, and again the
behavior I expected is not guarenteed! Wow! Is this suprising to any
of you guys?

Felix


Nov 16 '05 #2

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

Similar topics

11
by: Martin Jørgensen | last post by:
Hi, I'm using this alloc_mem-function: - - - - - - - - - - - - - - - - - - - - - - - - void *alloc_mem (size_t num_elems, size_t elem_size, char *filename, int line, size_t *total_mem) {
28
by: James Brown | last post by:
All, I have a series of characters which I need to convert to integer values. Each character is read in turn from a function 'nextch', and hex-digits are identified by the isxdigit function - so...
8
by: Roman Mashak | last post by:
Hello, this piece of code, compiled with gcc-3.4.4 (-ansi -pedantic -W -Wall), doesn't generate any warnings regarding conversion from 'double' to 'unsigned int'. Is it normal or a bug of...
0
by: maheshmohta | last post by:
Background Often while remodeling legacy application, one of the important tasks for the architects is to have an optimum usage of storage capabilities of database. Most of the legacy applications...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.