0

The program takes for parameter one double and its doing computation with long float point values. eg double myvar= n*0.000005478554 /298477. The problem is that im not sure that the real computational value is inserted to myvar. because whenvever i change n it produce the same thing cout<<"myvar ="<<myvar; What is the biggest type in c++ that can be used for maximum accuracy? Does a buffer overflow caused by this code because double variable cant hold too much info ? If yes what can happen and how can i detect it for later use?

Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83
Finlay Lifny
  • 43
  • 1
  • 6
  • 5
    The "biggest" floating-point type in C or C++ is `long double`. If you want more precision than that you have to use an external library such as [GMP](http://gmplib.org/). – Some programmer dude Feb 13 '14 at 08:54
  • If you're working in C++, then you can design a Rational-Number class, which maintains {numerator,denominator}, and performs all the arithmetic operations on them. So it essentially gives you "infinite" precision, until you print it (when you need to specify how many digits you want to print after the decimal point). Here is an example that you can follow (or use): http://planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=9735&lngWId=3 – barak manos Feb 13 '14 at 09:00
  • long double is the same as double? what is his max size? answer my other questions please – Finlay Lifny Feb 13 '14 at 09:03
  • 1
    Try this in your code: `printf("%u %u\n",sizeof(double),sizeof(long double));` – barak manos Feb 13 '14 at 09:04
  • 1
    @barakmanos: That should be `"%zu %zu"`, since `sizeof()` evaluates to type `size_t`, not `unsigned int`. Nitpicking, I know... up until you stumble across some strange machine where those two differ in size. – DevSolar Feb 13 '14 at 09:09
  • @FinlayLifny: You might want to check out `` and ``, headers that answer most of your questions. – DevSolar Feb 13 '14 at 09:13
  • You should also look at [std::setprecision](http://en.cppreference.com/w/cpp/io/manip/setprecision) – Retired Ninja Feb 13 '14 at 09:28
  • @DevSolar: a strange machine such as a Win64 machine (not that MSVC supports `"%zu"` anyway - but MinGW might). Sometimes it's easiest to just cast the `sizeof` result to `(unsigned)`. – Michael Burr Feb 13 '14 at 09:44
  • 2
    @MichaelBurr: I live in the relative luxury of not giving a damn about what MSVC supports. That compiler is a joke IMHO. ;-) – DevSolar Feb 13 '14 at 09:56

1 Answers1

3

double will hold values much smaller (and bigger) than 0.000005478554 /298477. Any problem that you have is almost certainly caused by a bug in your code. Show it!

Try to reduce your problem to a few lines. This kind of problems can be reproduced with something as small as

#include <iostream>
int main() {
  double myvar = 7 * 0.000005478554 /298477;
  std::cout << myvar;
}
MSalters
  • 173,980
  • 10
  • 155
  • 350
  • what do you mean by much smaller and bigger? – Finlay Lifny Feb 13 '14 at 12:19
  • 1
    The smallest positive value is certainly less then 1E-37 (about a billion billion billion times smaller than your number). Often, the smallest possible value is less than 1E-300. And the biggest `double` value is bigger than 1E+37, probably bigger than 1E+300. – MSalters Feb 13 '14 at 12:34
  • @FinlayLifny: It means "you are probably doing it wrong". Size is not an issue, only precision. 0.000005478554/298477 is roughly 1.8355029030712584219219571357257008077674326665036E-11 which obviously cannot be represented by a `double`, only approximated (even if those 50 digits were _all_ digits, which they aren't). The important question is: _What are you trying to do_? Usually, when people ask this kind of question about precision, they do not properly understand what floating point math is about. – Damon Feb 13 '14 at 12:37
  • If my variable is double and i try to make computation with long digit number. What will happen to those other digits who cant get into the buffer? what myvariable will represent ? the number without those extra digits?? – Finlay Lifny Feb 13 '14 at 14:20
  • @FinlayLifny: You don't even need inputs with many digits. `1.0 / 3.0` won't fit either. The least significant digits are dropped. – MSalters Feb 13 '14 at 14:32
  • so it get filled with what it can be filled from most to left significant bits? – Finlay Lifny Feb 13 '14 at 14:34
  • @FinlayLifny: Yes. The exponent says where the first non-zero bit is, and the next 53 or so bits are stored (exact number depends on implementation, 53 is the common IEEE754 format) – MSalters Feb 13 '14 at 14:37