## Math function with C

Math function with C
# 1
10-26-2012
Math function with C

I have

where int/float remains a float. How ever if I change it to

kilometers = 1.609 * (miles + yards / 1760);

where int/int returns 0. Where 385/1760 = .218.. which if this was declared of type int I understand the 0 result.
However kilometers is a float, so I figured all values whether whole or fractional could be handle. So is there a order of operations to data types? Or is it that compiler determines that 1760 is of type int. Where int/int remains an int.
 Fingerz View Public Profile for Fingerz Find all posts by Fingerz
# 2
10-26-2012
Are you printing it as a float...printf("%f\n", kilometers)
 shamrock View Public Profile for shamrock Find all posts by shamrock
# 3
10-26-2012
printf

printf("\nA marathon is %f kilometers.\n\n", kilometers);
 Fingerz View Public Profile for Fingerz Find all posts by Fingerz
# 4
10-26-2012
Dont know why it isnt printing the result as a float...get another compiler as this one is broken...btw what is your compiler type and version.
 shamrock View Public Profile for shamrock Find all posts by shamrock
# 5
10-26-2012
Compiler is gcc 4.5.2 on Solaris11 Box

It prints fine however given the 2 different formulas it prints 2 different results.

kilometers = 1.609 * (miles + yards / 1760.0);
end result = A marathon is 42.185970 kilometers.

kilometers1 = 1.609 * (miles + yards / 1760);
end result = A marathon is 41.834000 kilometers.

From what I gather when yards is divided by a float it results in a float.
When yards is divided by an int it results in a int
As a float 385/1760 = .218
As an int .218 = 0
 Fingerz View Public Profile for Fingerz Find all posts by Fingerz
# 6
10-26-2012
When you are performing operations on two integers, the result is an integer. So with:
and the precedence rules of C you have the following operations:
but with:
you have:
So the results you are seeing is exactly what is expected in the C language.

The type of the object that will be assigned the result of a calculation doesn't matter until you perform the assignment operation (= in this case). So, for examplekilometers = 1/2 + 3/4;produces a floating point 0.0 in kilometers because 1/2 is 0 and 3-4 is 0, 0 + 0 is 0 and (int) 0 converted to a float by the assignment is (float)0.0. On the other handmiles = 1./2 + 3/4.produces an integer 1 because 1./2 is 0.5, 3/4. is 0.75, 0.5+.75 is 1.25, and 1.25 converted to an int by the assigment to an int is (int)1.25 which has 1 as its integer value.
These 3 Users Gave Thanks to Don Cragun For This Post:
 Don Cragun View Public Profile for Don Cragun Find all posts by Don Cragun
# 7
10-26-2012
What going on

Ok been at this most of the day. The book I am reading stated that this was a bug.

Given a binary arithmetic operator, and arguments of different types, the compiler will treat both as if they're of the higher precision.
Remember that each operator in an expression is a separate evaulation. Compliments of jon.kiparsky from Dreaming In Code.

To me seems a very important aspect to remember and understand. I asked about the insides to what was happening because I can fore see when dealing with queries dealing with different numerical data types, this could cause many wasted hours on unwanted results. Thanks all for the time an explanations
 Fingerz View Public Profile for Fingerz Find all posts by Fingerz

## Math

i have file (my_file.txt) that looks like this: 000000000000010000 000000000000010000 000000000000005000 000000000000005000 000000000000005000 000000000000005000 000000000000005000 000000000000005000 000000000000005000 000000000000005000 all said and one, it should look...

## Linux Math Help

I am struggling with scripting this challenge a friend and I have. You have file1 and its contents is a single number you have file 2 and its contents are a different number you want to add file1 to file2 and have the output be put into file3

## Need help with AWK math

I am trying to do some math, so that I can compare the average of six numbers to a variable. Here is what it looks like (note that when I divide really big numbers, it isn't a real number): \$ tail -n 6 named.stats | awk -F\, '{print\$1}' 1141804 1140566 1139429 1134210 1084682 895045...

## math help

\$ x=1 \$ y=1.5 \$ z=\$((x*y)) bash: 1.5: syntax error: invalid arithmetic operator (error token is ".5") What's wrong?

## math in unix

I have 2 variables a=2 b=1 i want to add a and b how do i do this in unix using just the echo command and by assigning it to a different variable like c?

## math.h in makefile

Hey all, How do I link the math library in a gnu make makefile? I have tried using -lm with the CFLAGS varibale - flags like -Wall and -ggdb work, but -lm does not. I am running gcc - 4.1.2 on a linux machine.

## some math problems in C

I want to calculate secant method using C language That is a program----> #include<stdio.h> #include<math.h> #include<stdlib.h> main() { double fx(double x); double x0,x1,x2,f0,f1,f2,err; int n,i; printf("\n\n f(x) =x*x*x-5*x-7"); printf("\n\nEnter an interval in" ...