I have a function that uses an int, say 2488, to store temperature values. I have to call a function getTemp() to get the int. The getTemp function returns a double and uses the int to return the correct number. All the getTemp() does is return (double)x / 100.0 where x is 2488 in this case.
The returned double is then 24.88. This value then is sent to another function that adds the double to an array. Function is called DAaddDouble(double m, int x, int y) where m is the value to add, x and y is the coordinates that specify where to add the double.
Problem is, it turns into nan.
double a = getTemp();
//a is correct, i.e. 24.88
DAaddDouble(a, x, y);
/*-----------inside DAaddDouble----------*/
void DAaddDouble(double m, int x, int y)
{
//at this point, a (or m, same) is 0.nan
cord = x + y*40; //where to put the double
snprintf(DARRAY[cord], 5, "%f",m);
printf(....DARRAY[cord]...);
}
output: -nan
The signature of function DAaddDouble is:
void DAaddDouble(int m, int x, int y)
Note that m is of type int, and inside the function, you have:
snprintf(DARRAY[cord], 5, "%f",m);
in which %f expects type double, it's undefined behavior.
Probably what you need is to have the parameter m as double(as in your words above the code).
void DAaddDouble(double m, int x, int y)
Mis-match prototype.
Usage of DAaddDouble() is not preceded by its declaration/definition, thus the compiler assumes the function is:
int DAaddDouble(int m, int x, int y);
Precede the usage of DAaddDouble() by the function definition or a function prototype.
void DAaddDouble(double m, int x, int y);
A good compiler will warn of this. Insure all warning are enabled.
How do you know m is NAN. If you have infered by the value "printed" to DARRAY[cord], take into account that the format string should be "%lf" and not "%f".
void DAaddDouble(double m, int x, int y)
{
//at this point, a (or m, same) is 0.nan
cord = x + y*40; //where to put the double
snprintf(DARRAY[cord], 5, "%f",m); /* should be "%lf" for printing doubles */
printf(....DARRAY[cord]...);
}
Related
Here is my code:
#include <stdio.h>
#include <math.h>
double Mul(double X,double Y,double Z)
{
Y=Y*pow(10,6);
Y=Y+Z;
X=(X*pow(10,12))+Y;
//X=114360000000000000+117239051145;
//X=Y;
return X;
}
int main()
{
double Hello=Mul(114360,117239,511432);
printf("%f",Hello);
return 0;
}
The output should be "114360117239511432" but I got "114360117239511424" I need to know why 511432 converts to 511424? and How can I solve this problem?
I suggest to get familiar with floating point inaccuracy. However you use decimal numbers as parameters, they are not integers. If you want to know more of the integer limits, please check the numeric limits.
Let me be more specific. Double type become inaccurate if the exponent other than 1. I modified a bit your code, to show what are the exact values.
double Mul(double X, double Y, double Z)
{
double YbutMore = Y * pow(10, 6);
// YbutMore = 117239000000.00000
double YandZ = YbutMore + Z;
// YandZ = 117239511432.00000
double Xpow12 = X * pow(10, 12);
// Xpow12 = 1.1436000000000000e+17
return Xpow12 + Y;
// returns 1.1436000000011723e+17
}
So it all begins when we do a X * pow(10, 12). The mantissa cannot hold this big number, so the exponent will be other than 1 that will cause the inaccuracy. Don't forget to check the double value memory model.
If you are intrested how to store accurate and large numbers, please see How to store extremely large numbers?
What is the output of the following C program?
#include <stdio.h>
void abc (float, float, float *);
void main() {
float y = 2.5;
abc (6.5, y, &y);
printf ("%f\n",y);
}
void abc (float x, float y, float *z) {
y = y - 1;
*z = *z + x;
}
a) 1.5
b) 2.5
c) 8.0
d) 9.0
e) 9.5
The answer is d) 9.0. Could someone please explain why this is? I thought it would be c) 8.0 since in the function I updated the value of y as per y=y-1 to become 1.5. Thank you!
The short answer is that the y in main and the y in abc have the same name, but they are different. The one in abc is just a copy.
First, notice that the first line of the function abc is useless, it modifies y, but never uses y after that. Since when you call a function, a copy of each argument is made, this doesn't modify the y in main. And now the y in abc isn't used at all, so we can remove it from the parameters.
So you could rewrite abc as such :
void abc (float x, float *z) {
*z = *z + x;
}
And since there's not much point in such a simple one-line function, we can rewrite your program like this :
void main() {
float y = 2.5;
y = y + 6.5;
printf ("%f\n",y);
}
Now the result is pretty obvious.
abc (6.5, y, &y);
Please note that here the last parameter is call by reference and you are passing the address of the variable y.
Later in the function abc() you do
*z = *z + x
*z = 2.5 which is called dereferencing the pointer and the value stored in the address the pointer is pointing to is 2.5 and x=6.5 as you are passing it by value.So
*z = 2.5 + 6.5 = 9.0
Now in main() the value of y=9.0 because the value stored in the address of the variable y was modified by the function call abc()
When calling abc (6.5, y, &y) inside the function it will look like this:
void abc (x = 6.5, y = 2.5, *z = 2.5) {
y = 2.5 - 1; // y = 1.5
*z = 2.5 + 6.5; // *z = 9.0
}
And *z inside the function is same as y outside the function, so y = 9.0.
Simple, the third argument is a pointer to y in main(). That y value is being printed later on.
inside abc(), float y is local variable. any changes made to its value is not reflected back in the caller parameter.
OTOH, as per the logic of pointers, *z retains the computed value inside abc() thereby reflecting back the value to main().
In short, the computed value inside abc() is reflected back using the third argument z.
Calculation for z ==> 6.5 + 2.5 = 9.0
I just want to assign the value of pow to a variable, I've used this
#include<stdio.h>
#include<math.h>
int main( void )
{
int x;
int y;
int z;
x=10;
y=2;
z = pow(x,y);
printf ("%d" , &z);
return 0;
}
but in output I get -1076813284 , I am sorry, but I just started learning C and in every tutorial everyone just print the value of pow, like
printf("Value 3.05 ^ 1.98 = %lf", pow(3.05, 1.98));
and I don't know how to assign it to a variable
printf ("%d" , &z);
prints the address of z (*), not its value. printf("%d", z) prints the value.
(*) Actually, the behavior is undefined and on a 64-bit CPU it will likely print half of the address.
&z is the address of the variable z. If you want to print out the value of z, then the code is simply
printf("%d", z);
You would use &z when reading a value into z, because scanf needs a pointer in order to modify your variable.
pow returns a double (and not a reference), you need to make your print statement:
printf ("%f" , z);
After changing z to a double:
double z;
pow returns double and it takes arguments of type double.
double pow(double x, double y)
You need %f specifier in printf and also remove & from z.
Another way is to cast the return value of pow to int and use %d
int z = (int)pow(x, y);
printf ("%d" , z);
When I execute this code it returns me 1610612736
void main(){
float a=3.3f;
int b=2;
printf("%d",a*b);
}
Why and how to fix this ?
edit : It's not even a matter of integer and float, if i replace int b=2: by float b=2.0f it return the same silly result
The result of the multiplication of a float and an int is a float. Besides that, it will get promoted to double when passing to printf. You need a %a, %e, %f or %g format. The %d format is used to print int types.
Editorial note: The return value of main should be int. Here's a fixed program:
#include <stdio.h>
int main(void)
{
float a = 3.3f;
int b = 2;
printf("%a\n", a * b);
printf("%e\n", a * b);
printf("%f\n", a * b);
printf("%g\n", a * b);
return 0;
}
and its output:
$ ./example
0x1.a66666p+2
6.600000e+00
6.600000
6.6
Alternately, you could also do
printf("%d\n", (int)(a*b));
and this would print the result you're (kind of) expecting.
You should always explicitly typecast the variables to match the format string, otherwise you could see some weird values printed.
programming C using xcode, here's the f
#include <stdio.h>
int multiply (int x, int y) {
return x * y;
}
int main()
{
float result = (float) multiply (0.2, 0.5);
printf("the result is %f", result);
}
I don't get the right value, I get 0.0000 !! I did the casting but I don't know whats wrong.
Your program multiplies 0 by 0.
multiply takes two int parameters, so your 0.2 and 0.5 are implicitly converted to int before making the call. That truncates both to 0.
Your typecast doesn't do anything in this program, since the return value of multiply (which is an int) will get implicitly converted during the assignment to result anyway.
You need to change the definition of multiply (or add a floating-point version and call that) if you want this program to work correctly.
The multiply () input arguments are int:
int multiply (int x, int y) {
and you have passed float as input arguments:
multiply (0.2, 0.5);
Hi there is a basic problem. As the numbers you are multiplying are floats but you are passing these into the function multiply as int's hence being rounded to 1 and 0.
This should work
#include <stdio.h>
int multiply (float x, float y) {
return x * y;
}
int main()
{
float result = (float) multiply (0.2, 0.5);
printf("the result is %f", result);
}