How to get fractions in an integer division? - c

How do you divide two integers and get a double or float answer in C?

You need to cast one or the other to a float or double.
int x = 1;
int y = 3;
// Before
x / y; // (0!)
// After
((double)x) / y; // (0.33333...)
x / ((double)y); // (0.33333...)
Of course, make sure that you are store the result of the division in a double or float! It doesn't do you any good if you store the result in another int.
Regarding #Chad's comment ("[tailsPerField setIntValue:tailsPer]"):
Don't pass a double or float to setIntValue when you have setDoubleValue, etc. available. That's probably the same issue as I mentioned in the comment, where you aren't using an explicit cast, and you're getting an invalid value because a double is being read as an int.
For example, on my system, the file:
#include <stdio.h>
int main()
{
double x = 3.14;
printf("%d", x);
return 0;
}
outputs:
1374389535
because the double was attempted to be read as an int.

Use type-casting.
For example,
main()
{
float a;
int b = 2, c = 3;
a = (float) b / (float) c; // This is type-casting
printf("%f", a);
}

Related

How to use correctly struct type in c? [duplicate]

How do you divide two integers and get a double or float answer in C?
You need to cast one or the other to a float or double.
int x = 1;
int y = 3;
// Before
x / y; // (0!)
// After
((double)x) / y; // (0.33333...)
x / ((double)y); // (0.33333...)
Of course, make sure that you are store the result of the division in a double or float! It doesn't do you any good if you store the result in another int.
Regarding #Chad's comment ("[tailsPerField setIntValue:tailsPer]"):
Don't pass a double or float to setIntValue when you have setDoubleValue, etc. available. That's probably the same issue as I mentioned in the comment, where you aren't using an explicit cast, and you're getting an invalid value because a double is being read as an int.
For example, on my system, the file:
#include <stdio.h>
int main()
{
double x = 3.14;
printf("%d", x);
return 0;
}
outputs:
1374389535
because the double was attempted to be read as an int.
Use type-casting.
For example,
main()
{
float a;
int b = 2, c = 3;
a = (float) b / (float) c; // This is type-casting
printf("%f", a);
}

Is there a function in C that can take the largest integral value of a variable?

I made a program that can get the largest integral of a float value:
#include <stdio.h>
float get_value(float a);
int main() {
float num = 4.58;
float new_val = get_value(num);
printf("%f \n", new_val);
}
float get_value(float a) {
int c = a;
for (int i = 0; i < 99; i++) {
a -= 0.01;
if (a == c) {
break;
}
}
return a;
}
It didn't work in the way I wanted it to be, so I want a shorthand of it instead of making a function.
So is there a function that I can use for this?
Use floor() if you want the lowest integer (closest to minus infinity) not exceeding the floating point value. Or use trunc() to get the smallest integer (closest to zero) not exceeding the magnitude of the fp value.
Also, note that .1 has a repeating representation in binary fp, so your function as written is always going to have problems. Just like 1/3 becomes .3333 in decimal.
You can use modf:
double integral, fractional;
double num = 4.58;
int result;
fractional = modf(num, &integral);
result = (int)integral;

Why is my code not printing answer in float? [duplicate]

This question already has answers here:
Why is this simple piece of code not working?
(5 answers)
Closed 5 years ago.
#include <stdio.h>
float div ( int a,int b, int c, float x );
int main()
{
int a,b,c,x;
a=250;
b=85;
c=25;
x=div (a,b,c,x);
printf("%d ", x);
}
// your code goes here
float div (int a,int b, int c, float x)
{
x=((a-b)/c);
return(x);
}
Because x is declared as int instead of float.
Your code:
int x;
...
printf("%d ", x);
What you need instead:
float x;
...
printf("%f ", x);
And your div function is wrong too:
float div (int a,int b, int c, float x)
{
x=((a-b)/c); // << this will perform an integer division
return(x); // and therefore x will be truncated, even if it is a float
}
You need this:
float div (int a,int b, int c, float x)
{
x = ( ((float)(a-b) ) / c);
return x;
}
The (float) before (a-b) is called a cast and will ensure that (a-b) will be treaded as a float number during the division by c.
BTW the function can be simplified, you don't need the x parameter:
float div (int a,int b, int c)
{
float x = ( ((float)(a-b) ) / c);
return x;
}
or even simpler:
float div (int a,int b, int c)
{
return ((float)(a-b) ) / c;
}
Look closely at the statement
x=((a-b)/c);
Note the operands, they are all integers, so the integer division is performed and then, the result is converted to floating type upon assignment. You don't want that.
In case you want a floating point division, you need to make sure one of the operands are of type float/ double.
You can use a cast, like
x=(((float)a-b)/c);
to force floating point arithmetic.
That said, in the main(), you MUST use proper conversion specifier for float, %f.
prints out an integer:
printf("%d ", x);
prints out an floating point number:
printf("%f", x);
also x must be from type float

numerical integration using a pointer to a function always return 0

I am trying to use the function that I was given by my professor to calculate the integral of a polynomial function (polynomial such as: ax^2+bx+c). the function is:
double numbericalIntegration(double a ,double b ,double(*func)(double)){
double delta = (b - a)/32;
double sum=0, x;
for(x= a+0.5*delta; x<b ; x+=delta)
{
sum+=(*func)(x);
}
return sum*delta;
}
I changed a lot in order to integrate a polynomial function. but I was get the answer 0. why is that? and I'd appreciate if anybody tried to correct my work. my code is:
double integralPoly(double x, double a, double b, double c){
return (a*pow(x,3))/3 +(b*pow(x,2))/2 + (c*x);
}
double numbericalIntegration(double a ,double b ,double(*func)(double,double,double,double), double firstNum, double secondNum, double thirdNum){
double delta = (b - a)/32;
double sum=0, x;
for(x= a+0.5*delta; x<b ; x+=delta)
{
sum+=(*func)(x, firstNum, secondNum, thirdNum);
}
return sum*delta;
}
int main()
{
double (*func)(double,double,double,double);
func = integralPoly;
double sum = numbericalIntegration(2,4,func,1,1,4);
printf("sum = %d",sum);
return 0;
}
You need to change two things. First your polynomial function doesn't make any sense. You said it needs to be in the form of ax^2+bx+c but in your code polynomial is (ax^3)/3+(bx^2)/2+c*x. Your function should be:
double integralPoly(double x, double a, double b, double c){
return (a*pow(x,2)) +(b*x) + c;
}
Also you need to change your printf. %d is integer type specifier and you need double, so you need to use %f for example:
printf("sum = %f",sum);
Now the output of your program is:
sum = 32.666016
which is correct for your parameters.

Convert two 32 bit floats to one 64 bit number and vice versa

I need to convert two 32 bit floats into one 64 bit number and vice versa in C.
What's the best way of achieving this?
I am a little confused as to the best way and if I should convert from a float in 32 bit to a long in 64 bit and vice versa.
Help appreciated.
If you're talking about combining them mathematically somehow (such as addition), you can just coerce them both to 64-bit first:
float pi = 3.141592653589;
float e = 2.718281828459;
double sum = (double)pi + (double)e;
I'm guessing that's not what you want since, as someone pointed out in the comments, it would be hard to get back to the original values if all you have is the sum of them.
If you're talking simply combining the bits sequentially, you can do something like:
#include <stdio.h>
union {
struct {
float f1;
float f2;
};
double d;
} xyzzy;
int main (void) {
xyzzy.f1 = 3.141592653589;
xyzzy.f2 = 2.718281828459;
double d2 = xyzzy.d;
printf ("%lf\n", xyzzy.d);
xyzzy.f1 = 0;
xyzzy.f2 = 0;
xyzzy.d = d2;
printf ("%f %f\n", xyzzy.f1, xyzzy.f2);
}
which outputs:
14.985018
3.141593 2.718282
although you should keep in mind that such behaviour (type punning) is implementation defined as to whether it'll work. In any case, if the float values are 32-bit size and alignment, the inner struct itself will almost certainly be 64-bit and you can use that instead of the double (in other words, use the struct and get rid of the enclosing union).
If you want some functions that'll do the grunt work for you, see below:
#include <stdio.h>
double cvtToDbl (float n1, float n2) {
struct { float n1; float n2; } s;
s.n1 = n1;
s.n2 = n2;
return *((double*)&s);
}
void cvtToFlts (double d, float *pn1, float *pn2) {
struct { float n1; float n2; } *ps = (void*)&d;
*pn1 = ps->n1;
*pn2 = ps->n2;
}
int main (void) {
float f1 = 0, f2 = 0;
double d = cvtToDbl (3.141592653589, 2.718281828459);
printf ("%lf\n", d);
cvtToFlts (d, &f1, &f2);
printf ("%f %f\n", f1, f2);
return 0;
}

Resources