Proper Decimal Problem? [duplicate] - c

This question already has answers here:
Two decimal places using printf( )
(4 answers)
Closed 8 years ago.
#include <stdio.h>
int main (){
float M=2E30, G=6.67E-11, m=6E24, r=1.5E11;
float F= (G*M*m)/(r*r);
printf("F is %f",F);
return 0;
}
I am trying to print the value of F with two decimal precision. Could anyone help me please?

For two decimal precision, change your printf statement as below
printf("F is %.2f", F);
read this for more information.

You should try this:
printf("F is %0.2f",F);
The 0 in %0.2f tells that the output need not to be right justified and the 2 after the decimal tells that there should be only 2 digits after the decimal in the output. Basically %0.nf allows us to set a precision of n digits after the decimal in the output.
Hope this helps.

Related

What is wrong with this expression on the code? [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 2 years ago.
I have written this piece of code in my computer and the result is 7 instead of 8 (the correct result ... I think).
I don't know why... Can somebody help me?
#include <stdio.h>
int main() {
int num;
num = (68/10.0 - 68/10)*10;
printf("the result %d", num);
return 0;
}
double typically represents exactly about 264 different numbers. 68/10.0 is not one of them,
As a binary64, 68/10.0 is about
6.7999999999999998223643161..., the closest value to 6.8 that is a multiple of a dyadic rational. # AntonH
68/10 is an integer division with a quotient of 6.
(68/10.0 - 68/10)*10 is thus about 7.9999999999999982236431606...
Assigning that to an int is 7 not 8 as the fraction is discarded even though it is very close to 8.
When converting a floating point value consider round to the the closest, rather than truncating.
num = lround((68/10.0 - 68/10)*10);

Floating Point Representation in C [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 4 years ago.
I'm not sure how floating point are represented in C, and how much of a precision someone could get.
In a c source file, I have the macro:
#define NUMBER 123.367
In the main function there are these 2 instructions:
float x = NUMBER;
printf("x is %f\n", x);
When I run it, I get:
x is 123.366997
Which is quite close to 123.367, but it kinda messes the purpose of the program.
Is there any way to round up x to the desired value? Or is this a flaw of floating point arithmetic-representation that can't be fixed?
You should use a double, not a float.
#include <stdio.h>
#define NUMBER 123.367
int main (void) {
float f = NUMBER;
double d = NUMBER;
printf("f is %f\n", f);
printf("d is %f\n", d);
return 0;
}
This will give you:
f is 123.366997
d is 123.367000
'double' uses 53 bits for precision, while a 'float' only uses 24.

Relational operator in C while using real variables [duplicate]

This question already has answers here:
strange output in comparison of float with float literal
(8 answers)
Is floating point math broken?
(31 answers)
Closed 5 years ago.
Why the output is not as expected?
#include <stdio.h>
void main(){
float a,b,c;
b=0.7;
if( b<0.7 )
printf(" It should NOT be here");
else
printf("It Should be here");
}
0.7 is double value. Try 0.7f in code. It should work.
Floating point numbers
have limited accuracy
some value doesn't exist. 0.7 is such value. On my platform is
0.699999988.
Reason: we write numbers as decimal, but internal are realised as binary. Many longer materials exist
https://en.wikipedia.org/wiki/Floating-point_arithmetic#Representable_numbers.2C_conversion_and_rounding
So behaviour You are surprised
Please Try the below Code, it works!!!:
Code
#include <stdio.h>
int main(void)
{
float a,b,c,temp;
temp=0.7;
b=0.7;
if( b<temp )
printf(" It should NOT be here");
else
printf("It Should be here");
}

using printf to print a floating number with varying precision [duplicate]

This question already has answers here:
Printf variable number of decimals in float
(3 answers)
Closed 6 years ago.
A simple question.
I want to print a floating point number with precision given input from the user, i.e. for num=2.34567 and prec=2, I should print 2.35 as the answer, and for prec=3, I should print 2.346. How can we achieve this? (prec is given input from the user during runtime).
Thanks in advance.
This is probably what you are looking for:
float num = 2.34567;
int prec = 3;
printf("%.*f", prec, num);

C: Specifying a variable number of decimal points for output of float in printf? [duplicate]

This question already has answers here:
Printf variable number of decimals in float
(3 answers)
Closed 7 years ago.
I'm just getting to grips with C. I'm trying to print a floating point number, which I am aware I can do as follows...
printf("Rate: %f.\n",rate);
But I want to print only a specific number of decimal points, specified by a variable "decimalPoints". The decimalPoints value is determined by reading a string and converting it to a float using sscanf. Thus it is not limited to a specific value so %.2f won't work for instance. But say the value of decimalPoints is 2, is there a way to go about doing this?
i.e. something of the following format?
int decimalPoints = 2;
printf("Rate: %{decimalPoints}f.\n",rate);
You can't specify how many floating point decimal places you want to print like this.
Instead of
printf("Rate: %{decimalPoints}f.\n",rate);
do
printf("Rate: %.*f\n", decimalPoints, rate);
%.*f used in printf will take the first integer argument after the string as number of decimals you want to print with your floating point number.
Some testing:
#include <stdio.h>
int main(void) {
float rate = 1.223321f;
int decimalPoints;
for (decimalPoints = 0; decimalPoints < 6; decimalPoints++)
{
printf("Rate: %.*f \n", decimalPoints, rate);
}
}
Output:
Rate: 1
Rate: 1.2
Rate: 1.22
Rate: 1.223
Rate: 1.2233
Rate: 1.22332

Resources