Store a long decimal in C [closed] - c

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I am trying to do a physics problem and need to store a value around 5 * 10-11;
After trying float, long double and a few others none of them see to be long enough. Is there a data type that will allow me to do so?
Thanks
long double I = 0;
I = 0.01902*pow(0.00318,3)/12;
printf("%Lf\n",I);
Output is 0.000000

long double I = 0;
I = 0.01902*pow(0.00318,3)/12;
At this moment, I's value is approximately 5.096953e-11. Then...
printf("%Lf\n", I);
The sole format specifier in this printf() call is %Lf. This indicates that the argument is a long double (L), and that it should be printed as a floating-point number (f). Finally, as the precision (number of digits printed after the period) is not explicitly given, it is assumed to be 6. This means that up to 6 digits will be printed after the period.
There are several ways to fix this. Two of them would be...
printf(".15Lf\n", I);
This will set the precision to be 15. As such, 15 digits will be printed after the period. And...
printf("%Le\n", I);
This will print the number in scientific notation, that is, 5.096953e-11. It too can be configured to print more digits if you want them.

Related

How to create one only decimal number in C? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 12 months ago.
Improve this question
I've a problem: I would need to concatenate 3 double numbers into one only double number. For example, I've:
a = 40.000000;
b = 56.000000;
c = 10.236330;
I need the following number: 40.5610236330. The integer part is defined by the first two cyphers of a, the first two decimal cyphers are the integer part of b and the other decimal cyphers are all the cyphers of c. I've tried with:
k = a+(b/100)+(c/1000);
But due to approximation error, the result is 40.570236. Could you help me? Thank you so much!
Floating point calculation always loose some precision.
But 40.570236 instead of 40.5610236330 is too much off.
The big error you see is because of a simple bug in your code.
You need k = a+(b/100)+(c/10000); (i.e. c is to be divided by 10000)
Maybe it would be more clear if you did k = a+(b/100)+(c/100/100);
But never expect floating point calculation to 100% precise. It's not even certain that the number 40.5610236330 can be represented in float/double
And further, the input values them self may be imprecise:
double c = 10.236330;
printf("%.20f\n", c);
Output:
10.23633000000000059515
There is enough precision in a double variable to store a number of 12 significant digits (though your question does not really state how many digits c has).
double k= a + b * 0.01 + c * 0.0001;
will work. But when you display it, be sure to use a format with 10 digits after the decimal point (%.10f) so that rounding restores the correct decimals.

Why doesn't roundf work for me in this case? [closed]

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 2 years ago.
Improve this question
This is C. I am a beginner, so sorry for the experts to whom this question may seem trivial. I am trying to round this float to the nearest integer, away from zero. I've also tried rintf based on some other posts on the internet, but it just won't work! I used printf to check the results, and they weren't rounded to the nearest integer.
//Approximate US grade level.
float index = 0.0588 * L - 0.296 * S - 15.8;
float roundf(float index);
Note that
float roundf(float index);
is a declaration of a function. It is not a call.
If you use float roundf(float index); as function call inside of index = float roundf(float index); you should get a compiler error, but maybe you are on an uncommon compiler. Thus it can be a reason that it "won't work" as expected.
A correct call would be index = round(index);.
I used printf to check the results, and they weren't rounded to the nearest integer.
Note that floating-point precision isn't the best one in case you want to represent integers with it. A float or double can't represent an even integer value fully accurate. It has only a narrowed and limited precision.
Related:
Why not use Double or Float to represent currency?
Functions in C take an input and return an output in this way: output = function(input);. There may be more than one input for some functions, of course, but the principle is that.
For your case, try index = roundf(index); if you want the result to overwrite the non-rounded value.

Floating point to string representation [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Consider the following code snippet:
char str[1000];
float b ;
b= 0.0615;
sprintf( &(str[0]), "%1.0e", b);
After the execution of the last statement, I expected the str to contain 6.15e-2. However, I am getting the value as 5e-315.
Where am I going wrong. How to get the expected value?
You cannot get two digits precision with that format string, as you specified only one digit after comma (that is the .0 part after the 1).
What works for me is
#include <stdio.h>
#include <string.h>
main() {
char str[1000];
float b ;
b= 0.0615;
sprintf( &(str[0]), "%.2e", b);
puts(str);
}
prints 6.15e-02
The almighty C/C++ documentation says:
.number:
For a, A, e, E, f and F specifiers: this is the number of digits to be
printed after the decimal point (by default, this is 6).
My bet is you forgot including stdio.h.
There seems to be a mismatch between what type the compiler passes to sprintf and what sprintf actually reads (as described in cmaster's answer).
Apparently your compiler does not realize that sprintf() takes all floating point arguments as doubles. Consequently, it passes only the 32 bits of the float to the function, which erroneously interpretes 64 bits as a double. It should work fine if you cast the float to a double before passing it to sprintf().

Avoid precision loss for double [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I have the following code :
func(double val) {
// I am trying with the following values. both of which are in the range as per
// IEEE 754 std.
// val = 1.847474
int temp = [some_val = (1 << 23)];
double temp2 = val * temp;
printf("the produt111a = %15f\n",temp2);
}
value in temp2 results in loss of precision.
However, if I directly substitute the value of val while doing multiplication I got the correct result.
What can be done to avoid precision loss in such a scenario?
Precision is the (relative) difference from one floating point number to the next.
Accuracy is the (relative) difference between the numerical result and the exact result.
Multiplication with a power of 2 changes the exponent part of the floating point number and leaves the mantissa bits unchanged (see also the earlier comment by David Hammen). Thus there should be neither a loss in relative precision (still the same f.p. number type) nor in relative accuracy. Except in cases where you are very close to numerical over- or underflow.

Unsigned int overflow [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
I am facing an unsigned integer over flow issue
i.e unsigned int x= < max value * max value >
when I print x it is giving me the -ve value even though it is of unsigned integer
I am eager to understands how the compiler is making that as a negative vale
how do I over come this problem ??
thank you in advance
The compiler itself is not treating it as a signed value, that's almost certainly because you're using the %d format string for outputting it.
Use the %u one for unsigned decimal values and you see it have the "right" value (right in terms of signedness, not right in terms of magnitude, which will be wrong because you've performed an operation leading to overflow).
How are you printing it? Probably using printf. printf prints your unsigned ints as if they were signed (at least if you use %d). But this doesn't change the fact that the number is unsigned and hence positive.
Here's how you can check it: compare it to 0 and see what happens. So add this right after your printf:
if (x>=0) printf("positive\n");
else printf("negative\n");
and see what happens.

Resources