-3

The output answer is supposed to be 58.493745, the answer I keep getting is 584.937

#include <iostream>
using namespace std;
int main()
{
     double e = 2.0000000000 * 10^10;
     double r = 2.00000035 * 10^10;
     double c = 6.685 * 10^8;
     double v = c * (r - e) / (r + e);
     cout << v << endl;
     return 0;
}
  • `cout << "584.937"` ?? You don't tell what the task is, what is the algorithm supposed to do so we can't answer better than this. – bolov Aug 30 '20 at 00:05
  • 4
    `2.0000000000 * 10^10` should be `2.0000000000e10` – Eljay Aug 30 '20 at 00:07
  • @bolov sorry about that, I'm fairly new to this site. Its a radar gun problem. It is supposed to get the velocity from the data that was give. – Chris Tian Aug 30 '20 at 00:10
  • @Eljay I still get the same answer. – Chris Tian Aug 30 '20 at 00:12
  • 1
    Also replace `2.00000035 * 10^10` and `6.685 * 10^8` to `2.00000035e10` and `6.685e8`, and [I got](https://wandbox.org/permlink/8JOOkJT0zlPNUtzg) `58.4937`. Then you should add `cout.precision(8);` before `cout << v << endl;`. – MikeCAT Aug 30 '20 at 00:15
  • Running the numbers through a scientific calculator shows that the ideally-correct result of the math is `58.4937448817973228427`3. Running the program (with the exponents expressed properly as per Eljay's comment) prints the value as `58.4937`. The discrepancy is due to the printing function rounding off the displayed value to a smaller number of digits (and probably there is some round-off error due to using floating-point math, also) – Jeremy Friesner Aug 30 '20 at 00:20
  • By the way, I got [compilation error](https://wandbox.org/permlink/huyslBRKtzDJwyQL) from your original code. How did you get the answer 584.937? – MikeCAT Aug 30 '20 at 00:20
  • @MikeCAT i've copied and pasted the same code you gave me and still got the same output. Idk why this is happening, could it be that codeblocks settings might be off? – Chris Tian Aug 30 '20 at 00:25
  • @ChrisTian Did you compile the pasted code? – MikeCAT Aug 30 '20 at 00:26
  • I'm new at this. Idk how to compile to be honest. This is the first project we were given. @MikeCAT – Chris Tian Aug 30 '20 at 00:27

2 Answers2

4

With 10^10 you perform 10 XOR 10. There's no built-in power operator in C, but you can use 1e10 instead. Or even better put the e10 directly behind your literal.

yar
  • 1,855
  • 13
  • 26
1

Try the following code. I have replaced the " 10^x" with "ex"s per Eljay's comment.

#include <iostream>
using namespace std;
int main()
{
     double e = 2.0000000000e10;
     double r = 2.00000035e10;
     double c = 6.685e8;
     double v = c * (r - e) / (r + e);
     cout << v << endl;
     return 0;
}

Run this code online: https://www.onlinegdb.com/SJwUbOOQP

Evan Baldonado
  • 348
  • 1
  • 2
  • 13