0

So I'm trying to find the result of 1.25 ÷ (2 ✕ 10^-12). I first tried doing it with python, but the result was too big it returned a negative number. So I tried doing it in C, using a long double, but it's still not enough.

PS. I'm using GCC v9.2.1 on Linux Ubuntu

2 Answers2

1

Don't see anything wrong with python:

>>> 1.25 / (2 * 10**-12)
625000000000.0
lenik
  • 23,228
  • 4
  • 34
  • 43
-1

long long won't go bigger than a long double, but for your calculation, it's fine:

#include "stdio.h"
int main(){
    long long a = 1.25 / 2e-12;
    printf("%lld\n", a);
}

This prints 625000000000 for me.

Blaze
  • 16,736
  • 2
  • 25
  • 44
  • Why doesn't it give the same with result when I use `^`? – VirtualCode Jan 09 '20 at 11:20
  • @VirtualCode that's because the `^` operator isn't for exponentiation, but [bitwise XOR](https://www.geeksforgeeks.org/bitwise-operators-in-c-cpp/). `10 ^ 12` is `6`, because their bit pattern are `1010` respectively `1100`, so xor'd they are `0110`, which is the bit pattern for `6`. – Blaze Jan 09 '20 at 11:24
  • `2 ✕ 10^-12 = 2e-12`, not `2 * 10e-12`. And the expression is done in double precision. To do in `long double` use `1.25L/2e-12L` – phuclv Jan 09 '20 at 11:30
  • This is not a correct method. `2e-12` or `2e-12l` may round up when converted from decimal to the internal floating-point format, the division may round down, and then the result will not be `625000000000`. – Eric Postpischil Jan 09 '20 at 18:15