-2

Write a program that evaluates the expression

3.31 × 10^(-8) + 2.01 × 10^(-7) ÷ 7.16 × 10^(-6) + 2.01 × 10^(-8)

Answer

float result;

result = (3.31 * 10.0e + 2.01 * 10.0e-8) / (7.16 * 10.0e-6 + 2.01 * 10.0e-8);

NSLog(@"%e",result);

This is the answer I found but please explain the exponents part and why i need the 'e' also is there any other way of doing to the power of negative numbers. I tried doing it in other ways but it didn't work

eylay
  • 1,712
  • 4
  • 30
  • 54

3 Answers3

1

In C (and by extension, Objective-C), 3×105 is written 3e5. It's simply how scientific notation is expressed in C. Therefore, you can write:

float result = 3.31e-8 + 2.01e-7 / 7.16e-6 + 2.01e-8;
NSLog(@"%e", result);

Note that your expression in C should only have parentheses if your original mathematic expression did as well.

dreamlax
  • 93,976
  • 29
  • 161
  • 209
1

Use double, not float. float only gives you less than 8 digits precision at best. Unless you can explain with some good reason why you are using float, use double.

The traditional way to write numbers in "scientific format", in C since about 1970, but a lot earlier in FORTRAN, is 3.31E-8 for the first of your numbers, 2.01E-7 for the second and so on.

So:

double result = 3.31E-8 + 2.01E-7 / 7.16E-6 + 2.01E-8;

The parentheses that you wrote are wrong. Or your original formula is wrong, can't say which one.

gnasher729
  • 51,477
  • 5
  • 75
  • 98
0

The Answer must be

result = (3.31e-8 + 2.01e-7) / (7.16e-6 + 2.01e-8);

You can use this stack exchange link to understand better. In order to use less characters, instead of 10^(-8) or anything else programmers should write 10.0e-8.

Thats the best way to solve it.

Community
  • 1
  • 1
eylay
  • 1,712
  • 4
  • 30
  • 54