Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have been doing some research on the modf() function and I am having some trouble understanding on how to use it.
Code Example
#include<stdio.h>
#include<math.h>
int main ()
{
double x, fractpart, intpart;
x = 8.123456;
fractpart = modf(x, &intpart);
printf("Integral part = %lf\n", intpart);
printf("Fraction Part = %lf \n", fractpart);
return(0);
}
Confusion: The part I am getting confused on is the fractpart. I am having trouble understanding how it actually works because I only want to store the whole number part of a decimal number. Also, what is the point of the double.
First of, double is something really easy to understand. Float only stores seven digits, while a double stores between 15-16 digits. More detail here
Now for the modf. The modf in the fractpart =, it first checks x. Pretty much scanning it. After it does that, it stores everything before in the decimal point in inpart. After, what is remained is stored in fractpart which the decimal. To sum everything modf is like scanf. That is the reason why we put & before the intpart.
I only want to store the whole number part of a decimal number.
double modf(double value, double *iptr);
The modf functions break the argument value into integral and fractional parts, each of which has the same type and sign as the argument. They store the integral part in the object pointed to by iptr. C11dr §7.12.6.12 1
Simplified code follows. No need for fractpart or even intpart. Simply store the whole number portion of x back into x
#include<stdio.h>
#include<math.h>
int main () {
double x = 8.123456;
modf(x, &x);
printf("Integral part = %f\n", x);
}
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I'm trying to perform the following operation:
double exponent = pow(2.0, -254)
The result I get is 'inf', the actual result is: 3.4544e-77, which is a very small number, I would guess that I could get '0' instead but I get 'inf'.
I need the actual result, is there a way to improve precision on a double? I have tried also long double without success.
I'm programming on C with Visual Studio.
In C, you can multiply by powers of two with scalb, so scalb(1, -254) is 2−254. You can also use powers of two in hexadecimal floating-point constants; 2−254 is 0x1p-254.
However, pow(2.0, -254) does not return infinity. If you attempted to print the return value, and “inf” was printed, there is an error in your source code.
Yes, the problem was in my source code, my original code is:
#include <math.h>
void main()
{
unsigned int scale = 1;
unsigned int bias = 255;
double temp = pow(2.0, scale - bias);
printf("Number is: %e\n", temp);
return;
}
which Im guessing the operation 'scale-bias' is overflowing since they are UINTS, thus getting me a 'inf', changing them to INT solves the issue:
#include <math.h>
void main()
{
int scale = 1;
int bias = 255;
double temp = pow(2.0, scale - bias);
printf("Number is: %e\n", temp);
return;
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I've got multiple float numbers to print:
{3.00, 3.20, 2.23, 5.00, 3.40 }
They all have 2 decimal places, however if there are two zero's after a decimal, it needs to be displayed as a whole number.
How can I convert 3.00 into 3 and 5.00 into 5?
it is conversion not printing
There is no way to directly (i.e. using only the format specifier) print either two decimals or zero for a number ending in e.g. 3.20.
You need to check if the number ends with .00 beforehand; for example:
if((int)round(num * 100) % 100 == 0) // if decimals are `.00`
printf("%.0f", num);
else // not `.00`
printf("%.2f", num);
If 3.2 is okay for 3.20, then the %g specifier might also be of some use.
Just assign them to an int array or just type cast them in int for your purpose.
int i = 3.00; //lhs is int and rhs is double i will store 3 only
(int)3.00 //cast
printing them with %.f will also work
I think you cannot do this with a general format specifier or similar. You might need to treat each number separately and adjust the printf accordingly.
First you need to detect if you have digits that are 0.
You could do it like this
if (fabs(number - round(number)) < 0.01)
printf("%.2f", number);
else
printf("%d", (int) number);
This is just meant as a hint in the right direction. You might improve it a bit on your own.
Edit:
I assume that you want two digits for 3.20 and not 3.2 only.
Casting to integer type will truncate the positive integers down to integer part:
float f = 2.00;
printf("f: [%f], [%d]", f, (int) f);
f: [2.000000], [2]
If you want to leave 2.50 as 2.5 then checking for .00 is necessary:
if( (int) round(num * 100) % 100 == 0) // if decimals are .00
printf("%.0f", num); // truncate all
else // not .00
printf("%.2f", num); // truncate to 2 decimal places
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
i want to know if there is a code that can convert float number like : 4.91820238 to a float with one decimal place only : 4.9 in C without using any libraries
P.S: I don't use print() function , I want to convert it and sotre its value ?
You can use the fmodf function in the standard math.h header.
Then subtract the result from the original.
#include <stdio.h>
#include <math.h>
int main(void) {
float f = 4.9182;
printf("%f", f - fmodf(f, 0.1));
return 0;
}
Edit:
I see from your comment that you are on a freestanding environment. So no standard library. But you can probably whip up you own function by consulting the example implementation on the reference site, and doing some inline assembly.
Just multiply by 10 and divide by 10, do this
float a = 4.91820238;
int i;
i = a*10;
num = i/10.0;
// num has 4.9 now
Remember the 10.0 during the divison, if you just divide it by 10 you'll just get the integer quotient of it.
Convert it to an integer type and back again:
float myRound(float f, int num_places) {
float factor = 1f;
for (i = 0; i < num_places; i++) {
factor *= 10f;
return ((long long)(factor * (f + .5f)))/factor;
}
Note that the code above is subject to the usual precision/range problems regarding floating point numbers.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
for example length of 1.54 is 4 including the decimal point. i need a C code for finding the length in the same way but without strings and also snprintf
Calculating the length of a floating-point number decimal representation doesn't make much sense, as its underlying representation is binary. For example 0.1 in decimal form yields an infinite number of digits in binary.
C has no intrinsic decimal type, so you will have to represent your number with some ad-hoc type, and shave off insignificant digits on the right. C-strings are not the worse choice for that.
The length is set by you at the moment you print it.
#include <stdio.h>
int main ()
{
float x = 0.2;
double y = 0.2;
int size_x = sizeof(float);
int size_y = sizeof(double);
printf("Variable x has a size of %i. But i can print if as different lengths: %f, %3.2f, %g, %e\n", size_x, x, x, x, x);
printf("Variable y has a size of %i. But i can print if as different lengths: %lf, %3.2lf, %lg, %le\n", size_y, y, y, y, y);
return 0;
}
This question already has answers here:
Why adding these two double does not give correct answer? [duplicate]
(2 answers)
Closed 8 years ago.
I'm a bit of C newbie but this problem is really confusing me.
I have a variable double = 436553940.0000000000 (it was cast from an Int) and an other variable double 0.095832496.
My result should be 436553940.0958324*96*, however I get 436553940.0958324*67*.
Why does this happen and how can I prevent it from happening?
The number you expect is simply not representable by a double. The value you receive is instead a close approximation based on rounding results:
In [9]: 436553940.095832496
Out[9]: 436553940.09583247
In [18]: 436553940.095832496+2e-8
Out[18]: 436553940.09583247
In [19]: 436553940.095832496+3e-8
Out[19]: 436553940.0958325
In [20]: 436553940.095832496-2e-8
Out[20]: 436553940.09583247
In [21]: 436553940.095832496-3e-8
Out[21]: 436553940.0958324
You've just run out of significand bits.
Doubles are not able to represent every number. We can write some C++ code (that implements doubles in the same way) to show this.
#include <cstdio>
#include <cmath>
int main() {
double x = 436553940;
double y = 0.095832496;
double sum = x + y;
printf("prev: %50.50lf\n", std::nextafter(sum, 0));
printf("sum: %50.50lf\n", sum);
printf("next: %50.50lf\n", std::nextafter(sum, 500000000));
}
This code computes the sum of the two numbers you are talking about, and stores it as sum. We then compute the next representable double before that number, and after that number.
Here's the output:
[11:43am][wlynch#watermelon /tmp] ./foo
prev: 436553940.09583240747451782226562500000000000000000000000000
sum: 436553940.09583246707916259765625000000000000000000000000000
next: 436553940.09583252668380737304687500000000000000000000000000
So, we are not able to have the calculation equal 436553940.0958324_96_, because that number is not a valid double. So the IEEE-754 standard (and your compiler) defines some rules that tell us how the number should be rounded, to reach the nearest representable double.