0

I know that double has double the amount of memory as a float value but I was wondering if anyone can dig deeper to give me an easy to comprehend explanation. I am working on a program but for some reason a one decimal place value float doesn't work whereas double does. Why?

When you change the double maxSpecificGrowthRate, to float maxSpecificGrowthRate. It runs as though it passed through my if statement. When I print out the value it shows as 0.2 which should have been stopped by the if statement.

Code:

#include "stdafx.h
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
#include <fstream>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    float maxDilutionRate;
    double maxSpecificGrowthRate;
    float saturationConstant;
    int substrateConcentration;

    cout << "Enter maximum specific growth rate (per hour): ";
    cin >> maxSpecificGrowthRate;

    if (maxSpecificGrowthRate <= 0.2 || maxSpecificGrowthRate >= 0.7)
    {
        cout << "The maximum specific growth rate is not between 0.2-0.7 g/L. Terminating program..." << endl;
        system("pause");
        return 0;
    }

    system("pause");
    return 0;
}
user703016
  • 37,307
  • 8
  • 87
  • 112
Jonathan358
  • 39
  • 1
  • 4
  • @Patrick M'Bongo Oh, so by using float, the "empty" spaces are actually occupied by "garbage" giving it a slightly larger value than what is inputted? – Jonathan358 Oct 03 '16 at 02:12
  • No. `double` has the same fundamental properties as `float`. Whether you are using `float` or `double`, An irrational number like `.1` will have some "garbage", at some point. The only difference is that with a `double`, the garbage starts "later". – Sam Varshavchik Oct 03 '16 at 02:30
  • @Sam Varshavchik I see, thanks for the reply! I'm just going to use double from now on. I don't mind my files being a tiny bit larger. – Jonathan358 Oct 03 '16 at 02:35

0 Answers0