This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 4 years ago.
what i coded is
#include <stdio.h>
int main(){
float a, b;
printf("enter initial value.");
fflush(stdout);
scanf("%f", &a);
printf("add up the following number.");
fflush(stdout);
scanf("%f", &b);
printf("current value is %f.\n", (a+b));
fflush(stdout);
b = a+b;
printf("enter a value to subtract.");
fflush(stdout);
scanf("%f", &a);
printf("current value is %f. \n", b-a);
fflush(stdout);
b = b-a;
printf("enter a value to multiply.");
fflush(stdout);
scanf("%f", &a);
printf("current value is %f. \n", a*b);
fflush(stdout);
b = a*b;
printf("enter a value to devide.");
fflush(stdout);
scanf("%f", &a);
printf("current value is %f. \n", b/a);
fflush(stdout);
return 0;
}
Output:
enter initial value. 1000000
add up the following number. 9000000
current value is 10000000.000000.
enter a value to subtract. 0
current value is 10000000.000000.
enter a value to multiply. 1000000
current value is 10000000000000.000000.
enter a value to devide. 10
current value is 999999982796.800050.
https://i.stack.imgur.com/xuqab.png
i want 1000000000000.000000 as a result but 999999982796.800050 is all i've got....
what should i fix?
Single precision float can represent any real number for 6 decimal digits of precision. 999999982796.800050 meets that criteria.
This specific example may be fixed simply by using double, which is good for 15 decimal digits of precision.
If you must handle math with arbitrary precision for very large numbers, instead of using a float or double, look into an arbitrary precision library. A common on is called GMP
The idea behind an arbitrary precision library is the same as the idea behind how we do math as humans. A single "digit" can only store 10 possible values, but by creating an array of digits we can generate indiscriminately large values without losing accuracy.
Libraries like this utilize arrays and perform math one digit at a time in order to maintain perfect accuracy. They run a lot slower than floats or doubles, but if you care more about precision than speed then it's definitely the way to go.
Related
I'm attempting to create a simple calculator in C. For the calculator, I'm trying to include an option to switch between using double precision variables and integers. By default the calculator is in double precision mode.
Is there a way to easily toggle my data types back and forth between int and double? Preferably through type casting?
For example, here's the addition part of switch statement (the first case):
case 1:
printf("Enter first term: ");
scanf("%lf", &a);
printf("Enter second term: ");
scanf("%lf", &b);
printf("The sum is: %.15lf\n", a + b);
break;
The cases for subtracting, multiplying, and dividing follow the same format; respectively, cases 2, 3, and 4. Is there a way to apply a case (say add a case 5) to switch all double values to int?
Rather than trying to change the values, just output the result as an int:
printf("The sum is: %d\n", (int)(a + b));
I understand how to use pow() correctly I'm just wondering why when I run this code ans = inf.
I am having a hard time understanding this.
Does this have anything to do with chars only being able to take on the values -128 to +127 and the way pow() is calculated?
Does this have anything to do with the space in " %c" as in my first scanf param?
Linux 4.9.0-7-amd64
debian 4.9.110-1
gcc version 6.3.020170516
#include <stdio.h>
#include <math.h>
int main ()
{
char base, exp;
float ans;
printf("Enter base number : ");
scanf(" %c", &base);
printf("Enter exp number : ");
scanf(" %c", &exp);
ans = pow(base,exp);
printf("%c raised to the %c power equals %f", base, exp, ans);
return 0;
}
When you enter base and exp using %c you get the characters '0' - '9', not the integer values. So if you enter 0 and 0, the values you get (assuming ASCII encoding) will actually by 48, since that is the ASCII code for '0'. The result of 4848 is roughly 5 x 1080.
You then save the resulting value in a float. Assuming a float is IEEE754 single precision, it can hold an exponent no larger that +/- 38. So you're attempting convert an out-of-range value which invokes undefined behavior, meaning the result is unpredictable. If you changed the type of ans to double you would see the actual result.
Base and exp is not the integer values (0-9). So you want to enter "%d". Not the " %c"
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
This question already has answers here:
How do I restrict a float value to only two places after the decimal point in C?
(17 answers)
Closed 8 years ago.
I am writing a simple program to round numbers. It works fine when I enter something like 1.4 (gives me 1.0) or 2.6 (gives me 3.0). But when I enter 1.45, it should give me 1.5 but it prints 1.0. Or for 2.85 (should be 2.9, it prints 3.0). How do I make this work? I cannot use any functions.
#include<stdio.h>
const float add = 0.5;
int main(void)
{
float v;
printf("Please enter a value to be rounded: ");
scanf("%f", &v);
v = v + add;
v = (int)v;
printf("The rounded value is %.2f", v);
return 0;
}
Try this code. You need to break your floating value into Modulus and Dividend.
#include<stdio.h>
int main(void)
{
float v;
int con, r_value, mul;
printf("Please enter a value to be rounded: ");
scanf("%f", &v);
mul=v*10;
con=mul%10;
r_value=mul/10;
if(con>=5)
r_value=r_value+1;
else
r_value=r_value;
printf("The rounded value is %.2d", r_value);
return 0;
}
Above code is modified to be used for Negative Numbers also.
float v;
int con, r_value, mul;
printf("Please enter a value to be rounded: ");
scanf("%f", &v);
mul=v*10;
con=mul%10;
r_value=mul/10;
if(v>0)
{
if(con>=5)
r_value=r_value+1;
else
r_value=r_value;
}
else if (v<0)
{
if(con<=-5)
r_value=r_value-1;
else
r_value=r_value;
}
printf("The rounded value is %.2d", r_value);
Int value simply removes the value after decimal point it does't round of to nearest number
In your case when you give 2.45
2.45 + 0.5 = 2.95
which is rounded off to 2(2.0 since your asking for precision) i.e the decimal part is truncated not rounded off.
Ints do not have decimal places, so a cast from float to int then back to float will leave only the integer value.
Your subject (rounding to the second decimal place) does not match your question (rounding to an arbitrary position based on the input). That makes it difficult to answer your question properly.
That said, your code
v = v + 0.5;
v = (int)v;
will round a number to the nearest integer. I'm not sure why you expect the int cast to do anything else.
You are using v = (int)v;, how can it give 1.5 for input of 1.45.
To get what you want, you can just print the decimal digits 1 less than in original, i.e.
Suppose you have 2.455 which has 3 decimal digits, you can just print that number using %.2f and it will automatically be rounded off.
You can do:
printf("%.2f", ((int)(v * 100 + 0.5) / 100.0));
Obviously this is just a fraction of the code.
printf("Please enter a positive number that has a fractional part with three or more decimal places\n");
scanf("%5.2d", &x);
printf("The number you have entered is %5.2d\n" ,x);
Would this automatically round the number I type in? Or is there another way to do this?
Edit:
printf("Please enter a positive number that has a fractional part with three or more decimal places\n");
scanf("%lf", &x);
x = x + 0.05;
printf( "The number you have entered is %5.2lf\n", x);
Iv done this, but Im taking into consideration what someone had said about printf just "changing" the way it reads out. So this is obviously not the right way. Should I implement maybe the pow() function? Will that work with this somehow?
Edit2:
printf("Please enter a positive number that has a fractional part with three or more decimal places\n");
scanf("%lf", &x);
x = x + 0.05;
printf( "The number you have entered is %5.2lf\n", x);
Okay, iv gotten to the point where if i imput a number it will round to a whole number. 35.21 will round to 35, and 35.51 will round to 36 et. etc.
How would I get 35.2178 to round to 35.22, and 35.2135 to round to 35.21.
How would I get the certain powers of the decimal to round instead of the whole number?
You really, really should not store "rounded" values in floating point variables. Floating point inaccuracy will ruin this - your 5.10 might become 5.099999999941892 simply because the implementation might not be able to store 5.10 exactly.
As an alternative, read the whole number, multiply it with 100 and convert it to int (which will round it towards zero). That will keep your calculations accurate.
"%.2f" will round a double to 2 digits. A double is not an integer, and %d and %f are not interchangeable.
{
float x;
float rounded_x;
printf("Please enter a positive number that has a fractional part with three or more decimal places\n");
scanf("%f", &x);
rounded_x = ((int)(x * 100 + .5) / 100.0);
printf( "The number you have entered is %.2f\n", rounded_x);
return 0;
}
Thank you to everyone who tried to help! I finally got it
printf won't change the value of the number, just how it is displayed. An alternative is
#include <math.h>
// round double x to 2 decimal places
x = 0.01 * floor(x * 100.0 + 0.5);
This doesn't make sense
scanf("%5.2d", &x);
You can't have an integer with numbers after the decmal point. if x is a flat then weird things will happen. If its an integer why are you after 2 decimal places in the printf.
What exactly are you trying to do?
Edit:
double x;
printf( "Please enter a positive number that has a fractional part with three or more decimal places\n" );
scanf( "%lf", &x );
printf( "The number you have entered is %5.2f\n", x + 0.005 );
I'm pretty sure printf only truncates. So you will need to add 0.005 to round it.