tags:

views:

167

answers:

4

Those of you that are even moderately knowledgable of math will likely laugh, but I don't remember what much of the notation rules in math and I need assistance converting this into C code. Your help is greatly appreciated:

                                         214
          10,000 {(10,000 × [1+.0599/365]   )} +300
answer = ────────────────────────────────────────────
                                   214
                     .1+(1+(i/365))
A: 

Are you looking for the pow(x,y) function to calculate the power ?

Gangadhar
+1  A: 

Simple - just a couple of common mistakes to watch for.

Put a .0 after the constant numbers (especially in the denominator) so that 'c' treats the calculation as floating point math rather than integer.
In 'C' 100/1000 is 0 100/1000.0 is 0.1

Use brackets liberally- don't trust remembering the precedence rules.

You need to use * everywhere for multiplication 3(1+2) is not a multiplication.

The pow(x,y) function is used for x^y. Modern C++ compilers have optimized versions where y is an integer, so x^2 is much faster than x^2.0

Martin Beckett
I'm confused about the claim you made at the end. How is 2 distinguished from 2.0 as an argument to a function that takes a double? And why wouldn't both be equally subject to optimization?
R..
c++ can distinguish functions with the same name but different type args. Raising a number to a floating point power is quite complex, while an integer power can be done by simple multiplication.
Martin Beckett
ps. Sorry your question was about C, function overloading is specifically C++. In C people had to write their own squared(), cubed() functions to use multiplication for ^2 and ^3
Martin Beckett
A: 
(10000.0 ( ( 10000.0 * pow(1 + 0.0599/365.0, 214.0))) + 300.0 ) / (1 + pow(1 + i/365.0, 214.0))

I think I got that right. :)

Starkey
I think you're missing a multiplication sign after the first number. It's also quite hard to read though--it would be better broken into smaller parts on several lines.
Craig McQueen
+10  A: 

Are you looking for a program to translate that for you, or just a one-off conversion? If it's just a one off conversion, it's not that exciting.

Assuming I've read your syntax correctly:

double ans = 10000 * (10000 * pow(1.0 + 0.0599 / 365, 214) + 300;
ans /= (0.1 + pow(1.0 + (i / 365.0), 214));

I will say, though, that you may have an issue with raising things to that high of an exponent and dividing. More likely you will have to translate to logs and do your math in the log space, and convert afterwards.

What that might look like:

double lognumerator = log(10000) + log(10000) + 214 * log(1 + 0.0599 / 365);
double logdenominator = log(0.1 + exp(214 * log(1.0 + (i / 365.0))));
double ans = exp(lognumerator - logdenominator) + exp(log(300) - logdenominator);

The use of log may prevent you from hitting underflow, which you may very well hit with these types of computations.

Mike Axiak
`(1+.0599/365)^214` is 1.036, I wouldn't worry about it too much. `(1+(i/365))^214` starts getting big around i = 100.
Steve Jessop