c++mathroundingsignificant-digits

C++ significant figures


How can I do math involving significant figures in C++? I want this to work correct with measured data from chemistry and physics experiments. An example: 65 / 5 = 10. I would need to get rid of unneeded decimal places and replace some digits with 0s.

Thanks!


Solution

  • Well there are good math libraries in math.h

    Also storing your figures in floats, doubles or long doubles will allow for more precise operations.

    Floats offer 7 significant digits while doubles offer 16 significant digits.

    source

    Also when printing out usually people use _snprintf or printf and you can format those doubles, floats to the precision you want like:

    Float Precision

    printf("Value %8.2f", floatVariable);

    This says you require a total field of 8 characters, within the 8 characters the last 2 will hold the decimal part.

    _snprintf(buffer, sizeof(buffer), "Value %.2f", floatVariable);

    The example above requests the minimum field width and the last two characters are to hold the decimal part.