I am willing to cast precise operations and for that purpose I need a way to
seperate a float number into an integer and a fractional part.
Is there any way for this?
There is a function included in math.h library called modf
With this function you can do just what are you trying to.
Example:
#include <stdio.h>
#include <math.h>
double ftof ()
{
double floating = 3.40, fractional, integer;
fractional = modf(floating, &integer);
printf ("Floating: %g\nInteger: %g\nFractional: %g", floating, integer, fractional); // when using printf, there are no floats
return fractional;
}
Output:
Floating: 3.40
Integer: 3
Fractional: 0.40
Note that using double in most of the cases is better than using float, despite that double
consumes twice the memory of float (4:8 bytes) hence the increased range and accuracy. Also in case you need more precise output from
bigger floating numbers when printing, you can try the printf() exponent format specifier %e instead of %g which only uses the
shortest representation of the floating decimal.
One other way using type cast.
#include <stdio.h>
#include <math.h>
void main()
{
float a = 3.4;
float a_frac = a - (int) a;
float a_int = a - a_frac;
printf("Number = %f, Integer = %f, Fraction = %f", a, a_frac, a_int);
}
A thought crossed my mind to separate them with some logic :
#include <iostream>
using namespace std;
int main()
{
double fr,i,in,num=12.7;
for(i=0;i<num;i++)
{
fr=num-i;
}
cout<<"num: "<<num;
cout<<"\nfraction: "<<fr;
in=num-fr;
cout<<"\nInteger: "<<in;
}
Hope this was what you were searching for:) .
#include <bits/stdc++.h>
using namespace std;
int main()
{
double n;
cin>>n;
double fr = n-((int)n);
cout<<"int "<<(int) n<<"\n";
cout<<"fraction "<< fr;
return 0;
}
Related
Why does the C code below output "Difference: 0.000000" ? I need to make calculations with many decimals in one of my university tasks and I don't understand this because I'm new to programming in C. Am I using the correct type? Thanks in advance.
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <math.h>
int main() {
long double a = 1.00000001;
long double b = 1.00000000;
long double difference = a-b;
printf("Difference: %Lf", difference);
}
I have tried that code and I'm expecting to get the result: "Difference: 0.00000001"
You see 0.000000 because %Lf prints a fixed number of decimal places, and the default number is 6. In your case, the difference is 1 in the 8th decimal place, which shows as 0.000000 when printed to 6 d.p. Either use %Le or %Lg or specify more precision: %.8Lf.
#include <stdio.h>
int main(void)
{
long double a = 1.00000001;
long double b = 1.00000000;
long double difference = a - b;
printf("Difference: %Lf\n", difference);
printf("Difference: %.8Lf\n", difference);
printf("Difference: %Le\n", difference);
printf("Difference: %Lg\n", difference);
return 0;
}
Note the minimal set of headers.
Output:
Difference: 0.000000
Difference: 0.00000001
Difference: 1.000000e-08
Difference: 1e-08
#include <stdio.h>
int main() {
long double a = 1.000000001;
long double b = 1.000000000;
long double difference = a-b;
printf("Difference: %.9Lf\n", difference);
}
Try this code. Actually, you need to specify to the compiler how much precision you need after the decimal point. Here the .9 will print 9 digits after the decimal point. You can adjust this value according to your needs; just don't exceed the range of the variable.
I'm trying to split a number in C into two at the decimal point. For example, let's say the number is 1.5, then I want to split it into 1 and 0.5. How can I do this?
You can use modf() from math.h library:
#include <stdio.h>
#include <math.h>
int main () {
double value, fractional, integer;
value = 8.123456;
fractional = modf(value, &integer);
printf("Integral part = %lf\n", integer);
printf("Fraction Part = %lf \n", fractional);
return(0);
}
Sample from tutorialspoint.com
The C standard specifies a modf function, declared in <math.h>. This code puts the integer part of the double x in IntegerPart and the fraction part in FractionPart:
double IntegerPart;
double FractionPart = modf(x, &IntegerPart);
Each of IntegerPart and FractionPart will have the same sign as x.
This question already has answers here:
Why dividing two integers doesn't get a float? [duplicate]
(7 answers)
Closed 5 years ago.
input example : 356
356/100, is suppused to be 3.56
But I'm getting 3.0000000000, I'm using ideone online compiler for C.
#include <stdio.h>
#include <math.h>
int main() {
int n;
double frac;
scanf("%d", &n);
frac = (n/100);
printf("%lf", frac);
return 0;
}
That's because here frac = (n/100); you are doing plain integer arithmetic (as n is declared as an int and 100 is interpreted as an int (any whole number is taken to be an int unless specified otherwise)). What you need to do is say explicitly that you want to do an arithmetic operation with digits after decimal point. One way is to use a cast: frac = ((double) n/100);
If you don't use the cast, the division will be performed as you expect, but then the digits after the decimal point will be dropped. Since frac is declared as a double, 0s would get tacked on to the end.
In C, the result of division of two integer numbers (e.g. int, short, long) is also an integer (it is counter-intuitive, but it is implemented this way for performance reasons). As a result, the result of 5/2 is 2 and not 2.5. This rule is only for integer numbers. So, if you need to get a floating-point result, at least one of the numbers in a division operation must be of a floating-point type.
In case of your code, if you use 100.0 instead of 100, you will get the desired result. Also you can use casts or define n as double.
This should work:
#include <stdio.h>
#include <math.h>
int main() {
int n; // You can define n as double but don't forget to modify scanf accordingly.
double frac;
scanf("%d", &n);
frac = (n/((double)100)); // Or, frac = (n/100.0)
printf("%lf", frac);
return 0;
}
You cannot call division using integers to be double without declaring it.
For example
int / int will result int.
Try declaring n as double
double n;
scanf("%lf", &n);
frag = n/100;
input data type is an integer.
just change it to double or float to fix this problem.
int main() {
double n;
double frac;
scanf("%d", &n);
frac = (n/100);
printf("%lf", frac);
return 0;
}
I am new to C programming. I keep getting this error, (in this case relating to code inside the cubic root function):
1>c:\users\r\documents\visual studio 2010\projects\lab5.c\lab5.c\lab5code.c(57): warning C4244: '=' : conversion from 'double' to 'float', possible loss of data
I have tried storing the cubic root calculation in a float, and then returning that to main, but still no luck. I have seen people compile my code with no problems.
I have so far tested the code on VS 2008, and 2010 express editions, same errors. I get this a lot, trying to figure out why.
//INCLUDE HEADER FILES
#include <stdio.h> //Defines printf, scanf & getch
#include <conio.h> //Defines get.ch
#include <stdlib.h> //Defines system("pause")
#include <math.h> //Defines math functions
// FUNCTION PROTOTYPES
void explain();
float get_value();
float cubic_root(float num);
void display(float x, float y);
int main(void)
{
float in,out;
//Variable Declarations
explain(); //Explain
in=get_value(); //Get Value from USER
out=cubic_root(in); //Calculations
display(in,out); //Output
}
//FUNCTION DEFINITIONS
void explain(void)
{
system("cls");
puts("This will take cubic root\nPress enter to continue...");
_getch();
}
float get_value(void)
{
float input;
fflush(stdin);
puts("Enter the number you want to cube root...\n");
scanf_s("%f",&input);
return(input);
}
float cubic_root(float num)
{
float div,total;
total=(pow(num,1.0/3.0));
return(total);
}
void display(float x, float y)
{
printf("%.1f, %.1f",x,y);
getch();
}
Because pow() returns a double, and you are assigning it to a float.
exp1=pow(num,0.33);
pow() returns a double and you're converting it to a float. That's why a warning is emitted and you should take notice of it.
The best thing to do is to refactor your code to use double precision variables. You will probably find that there is no performance hit in doing that as many low level floating point computations are at (or higher than) double precision anyway.
Note that pow(num, 0.33); is a grotesque approximation for a cube root. Use pow(num, 1.0 / 3); instead. You need to use 1.0 so the literal is evaluated (most likely at compile time) in floating point.
This:
exp1 = pow(num, 0.33);
assigns the return value of the pow() function, which has type double, to exp which has type float.
The fix is to use the powf() function instead:
float cubic_root(float num)
{
return powf(num, 1.f / 3.f);
}
You can of course just cast the result to float to tell the compiler you really mean this, but it seems extremely wasteful and pointless to do the exponentiation calculation using more precision than you really need, so don't do that.
#include <stdio.h>
int main(){
float var = 0.612;
printf("%f\n",var);
printf("%f\n",var*100);
return 0;
}
o/p
0.612000
61.199997
I found that for JavaScript, we have .tofixed() method.
How do we get a fix for this in C?
You can specify the precision when printing:
printf("%.3f\n", 100 * var);
Since the exact number you're having probably isn't representable in the float itself, there is no operation you can do on the number itself to "remove" the decimals, it's all a matter of how you choose to present the data.