473,503 Members | 1,929 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to convert efficiently double to int

1 New Member
Hello all.

I have a function with paremeter double *col_zero that contains integger values. like 0, 1, 2. I am 100% sure of that.

I cant change the inputs and I have to use these values in a for loop for a count.

For now I am just printing my values in a file to be sure the output are as I want.

fp is a FILE pointer.
If I make :
fprintf(fp, " %d or %i \n ", row_zero[k],(int)row_zero[k] );

the output for the value 0 is
0 or 1074266112
0 or 1072693248

Could somebody explain me where the second value come from? Is my cast wrong?
If, so what is the most efficient way to deal with that??

Thank you
Oct 3 '11 #1
5 2960
weaknessforcats
9,208 Recognized Expert Moderator Expert
A double is a floating point type. It has a decimal portion. An integer does not have a decimal portion. Therefore, there is no way toconvert a double to an int. If you try you will be warned by the compiler about potential loss of data as the decimal portion is tossed away. All the cast does it tell the compiler to shut up.

You might try the ceil() function using your double rather than a cast.
Oct 4 '11 #2
Banfa
9,065 Recognized Expert Moderator Expert
In your fprinf format string you are using %d and %i. Both of these are for printing integers when you are trying to output the double you need to use %f

read this
Oct 4 '11 #3
donbock
2,426 Recognized Expert Top Contributor
What type is row_zero?
I'll assume it is double* (like col_zero).

The compiler will automatically convert function arguments to the types specified by the in-scope function prototype or function definition before passing the arguments to that function. When argument type information is not available (no function declaration, old-style function declaration/definition, variable number of arguments), the compiler performs the so-called usual conversions on arguments before passing them to that function. I listed my recollection of the usual conversions below. This may not be precisely correct, but it will give you the idea.
  • float or double arguments are converted to double;
  • char, unsigned char, short, unsigned short, and int are converted to int;
  • unsigned int is converted to unsigned int;
  • long is converted to long;
  • unsigned long is converted to unsigned long.

The function prototype for fprintf is
Expand|Select|Wrap|Line Numbers
  1. int fprintf(FILE *fp, const char *fmt, ...);
The first two arguments are converted to the prototyped types, but the last two arguments are subject to the usual conversions. The third argument is converted to double, the last is converted to int. The format string tells fprintf to interpret the third argument as an integer, but it is actually a double so you have undefined behavior -- anything can happen. The format string tells fprintf to interpret the last argument as an int, and it is an int so that one is handled properly.
Oct 4 '11 #4
Banfa
9,065 Recognized Expert Moderator Expert
The format string tells fprintf to interpret the third argument as an integer, but it is actually a double so you have undefined behavior -- anything can happen. The format string tells fprintf to interpret the last argument as an int, and it is an int so that one is handled properly.
Not quite Don :D

The compiler puts all variables passed to fprintf on the stack after the conversions you provide above. A double is normally 8 bytes and an int 4 so the compiler puts 12 bytes in total on the stack.

fprintf (and family) have no knowledge of the actual number of parameters passed, they just have a chunk of data (an array of bytes effectively) on the stack that they interpret according to the format string. The first format string parameter tells fprintf to display an int so it takes enough data from its chunk of data for an int and displays it as an int, this is 4 bytes which would be the first 1/2 of the double parameter. The second format string parameter, again, tells fprintf to display an int so it takes enough data from what's left of its chunk of data for an int and displays it as an int, this is 4 bytes which would be the second 1/2 of the double parameter. The last 4 bytes of the chunk of data is ignored.

So the program actually outputs the data contained in the double interpreted as 2 integers and ignores completely the actual integer passed.
Oct 5 '11 #5
donbock
2,426 Recognized Expert Top Contributor
The moral here is that expert or not, undefined behavior may be worse than you can imagine.
Oct 5 '11 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

6
2164
by: Marty | last post by:
Hi, If I don't know that my (or any) windows 2k regional setting for the decimal symbol is the dot (".") and I want to do this: 1- Dim dblOutput As Double 2- Dim strInput As...
12
2291
by: DanSteph | last post by:
hello all, I have a problem, I have a DOUBLE value like that 4065 4000 0000 0000 and I want to convert it to a DWORD like that: 4065 4000 if I do result=(DWORD)MyDouble it end with...
3
6716
by: Norvin Laudon | last post by:
Hi, Can somebody explain the following, from the MSDN documentation for the "System.Convert.ToInt32(double)" function <quote> Return Value value rounded to the nearest 32-bit signed...
17
4340
by: David Scemama | last post by:
Hi, I'm writing a program using VB.NET that needs to communicate with a DOS Pascal program than cannot be modified. The communication channel is through some file databases, and I have a huge...
3
7699
by: PeterK | last post by:
I am trying to set Public overridable CreditlimitS() as System.Data.SqlTypes.SqlMoney to Creditlimit as Double like CreditLimitS=creditlimit and get this error "Value of type double cannot be...
4
2373
by: Gav | last post by:
Hi all, I have a value in a database which is of float and in order to get my value from my textbox into my database I am doing something along the line of sqlparm1.value=...
6
16941
by: Doug | last post by:
Hi I have a short piece of trial code that compares some input and then produces a message based on the value. However I get a build error that i dont know how to resolve ' Cannot implicitly...
4
4502
by: Edwin Knoppert | last post by:
In my code i use the text from a textbox and convert it to a double value. I was using Convert.ToDouble() but i'm used to convert comma to dot. This way i can assure the text is correct. However...
3
63033
by: rishabhshrivastava | last post by:
Hello All, How can I convert a value to Double in JavaScript??? In vbscript i believe its done as cDbl(Value) I tried lots of way but getting a value of "NaN". Any suggestions/ideas will...
4
2542
by: Yoavo | last post by:
Hi, I want to convert a string to double. I use the function: "System.Convert.ToDouble". The problem is that if the string contains the character "." the program aborts. What might be the...
0
7192
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
7315
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...
1
6974
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
5559
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,...
1
4991
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...
0
4665
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3158
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3147
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1492
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.