0

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))
badp
  • 11,409
  • 3
  • 61
  • 89
james_womack
  • 10,028
  • 6
  • 55
  • 74
  • What does "10000 {something} +300 signify"? Is that like "10000*something + 300"? – Chuck Sep 14 '10 at 20:58
  • 1
    You don't have a consistent number of brackets in the denominator... – Oliver Charlesworth Sep 14 '10 at 20:58
  • 1
    try to be as verbose as possible. split sub statement to different temporary variables; it can make it easier to read and maintain. – Max Sep 14 '10 at 21:11
  • 1
    Make sure you name all the numbers within your program, even if they aren't variables (e.g. if they're constants, such as PI). Magic numbers are bad :) – Merlyn Morgan-Graham Sep 14 '10 at 22:13
  • 2
    Is the 2 in `struct node *children[2];` for a binary tree a "magic number"? Is the 0 in `for (i=0; ...` a "magic number"? Nonsensical rules against "magic numbers" in code considered harmful. – R.. GitHub STOP HELPING ICE Sep 15 '10 at 01:16
  • If you want to find the notation rules in math, then try searching ["order of operations"](https://www.google.com/#hl=en&sclient=psy-ab&q=order+of+operations&oq=order+&gs_l=hp.3.1.0i20j0l9.41038.43279.2.45132.7.6.0.1.1.0.83.453.6.6.0...0.0...1c.1.8.hp.qNWsqsKhlg4&psj=1&bav=on.2,or.r_cp.r_qf.&bvm=bv.44990110,d.dmQ&fp=e9443ae8fe5a8991&biw=1366&bih=639). – Anderson Green Apr 10 '13 at 21:04

4 Answers4

12

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
  • 11,827
  • 2
  • 33
  • 49
  • 3
    `(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 Sep 15 '10 at 00:01
2

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
  • 94,801
  • 28
  • 188
  • 263
  • 1
    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.. GitHub STOP HELPING ICE Sep 15 '10 at 01:20
  • 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 Sep 15 '10 at 03:45
  • 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 Sep 15 '10 at 04:38
0

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

Gangadhar
  • 1,893
  • 9
  • 9
0
(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
  • 9,673
  • 6
  • 31
  • 51
  • 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 Sep 14 '10 at 22:55