You're using the wrong format specifier to printf.
The %d format specifier expects an int argument, but you're passing a double. Using the wrong format specifier invokes undefined behavior.
To print a double, use %f.
printf("average: %f\n", average);
Answer from dbush on Stack OverflowYou're using the wrong format specifier to printf.
The %d format specifier expects an int argument, but you're passing a double. Using the wrong format specifier invokes undefined behavior.
To print a double, use %f.
printf("average: %f\n", average);
No need to modify the statement average=(double)(num1+num2)/2; to get expected result inside printf use %f instead of %d
- 1st
(num1+num2)is performed, result of this is ofintegraltype. lets say15. Next when you do(double)15/2result is offloatingtype which is7.500000. - from previous step
average = (double)7.500000average holds7.500000but since you printed in%dyou are getting0as its undefined behavior. instead use%f
Here is the working one
int main() {
int num1 = 7, num2 = 8;
double average;
average = (double)(num1 + num2)/2;
printf("average: %f\n", average);
return 0;
}
Data type conversion int to double withi - C++ Forum
c - Convert int to double - Stack Overflow
convert int to double
How to Convert double to int in C? - Stack Overflow
Videos
There's no double() function in Processing like there is for float() or int(). When I try to use Number.doubleValue() from directly from java.lang, I get an error message saying it "expects no parameters." When I try to convert an int using the Double class, it simply outputs the value as 100.0 instead what it actually should be.
I'm actually trying to make double versions of both width and height
please help
Maybe this?
int number;
double dblNumber = (double)number;
The problem is incorrect use of printf format - use %g/%f instead of %d
BTW - if you are wondering what your code did here is some abridged explanation that may help you in understanding:
printf routine has treated your floating point result of sqrt as integer. Signed, unsigned integers have their underlying bit representations (put simply - it's the way how they are 'encoded' in memory, registers etc). By specifying format to printf you tell it how it should decipher that bit pattern in specific memory area/register (depends on calling conventions etc). For example:
unsigned int myInt = 0xFFFFFFFF;
printf( "as signed=[%i] as unsigned=[%u]\n", myInt, myInt );
gives: "as signed=[-1] as unsigned=[4294967295]"
One bit pattern used but treated as signed first and unsigned later. Same applies to your code. You've told printf to treat bit pattern that was used to 'encode' floating point result of sqrt as integer. See this:
float myFloat = 8.0;
printf( "%08X\n", *((unsigned int*)&myFloat) );
prints: "41000000"
According to single precision floating point encoding format. 8.0 is simply (-1)^0*(1+fraction=0)*2^(exp=130-127)=2*3=8.0 but printed as int looks like just 41000000 (hex of course).
I suspect you don't actually have that problem - I suspect you've really got:
double a = callSomeFunction();
// Examine a in the debugger or via logging, and decide it's 3669.0
// Now cast
int b = (int) a;
// Now a is 3668
What makes me say that is that although it's true that many decimal values cannot be stored exactly in float or double, that doesn't hold for integers of this kind of magnitude. They can very easily be exactly represented in binary floating point form. (Very large integers can't always be exactly represented, but we're not dealing with a very large integer here.)
I strongly suspect that your double value is actually slightly less than 3669.0, but it's being displayed to you as 3669.0 by whatever diagnostic device you're using. The conversion to an integer value just performs truncation, not rounding - hence the issue.
Assuming your double type is an IEEE-754 64-bit type, the largest value which is less than 3669.0 is exactly
3668.99999999999954525264911353588104248046875
So if you're using any diagnostic approach where that value would be shown as 3669.0, then it's quite possible (probable, I'd say) that this is what's happening.
main() {
double a;
a=3669.0;
int b;
b=a;
printf("b is %d",b);
}
output is :b is 3669
when you write b=a; then its automatically converted in int
see on-line compiler result :
http://ideone.com/60T5b
This is called Implicit Type Conversion Read more here https://www.geeksforgeeks.org/implicit-type-conversion-in-c-with-examples/
You have two problems: The first have to do with the functions return type, and returning an int will always truncate a floating point value.
The second problem is that you don't return the result of the division, you return the value of the variable value after the assignment to it, and since value is an integer you will return the truncated value. Drop the assignment back to it: return (double) value / 2;
int Divide(int value)
change to
double Divide(double value)
since your value is of type integer, so it assigns an integer value to a float which will be 2.0
Hey,
i have two int variables (hours, minutes) and one double variable (driven miles/km).
I want to calculate the average speed (distancy / time).
I thought about: averagespeed = miles/km / (hours + (minutes / 60));
but that gives me the warning: different types in assignment (possible loss of date)
Is it possible to convert the two int variable into one double hours.minutes?