Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C Primer Plus Chapter 3 Programming Exercise 6

Tags:

c

I am using a Windows 11 laptop. When I run gcc --version, the computer outputs me the following:

gcc (MinGW-W64 x86_64-ucrt-posix-seh, built by Brecht Sanders, r1) 14.2.0
Copyright (C) 2024 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

When I declare the variable amount_molecules as float or double, the result I end up with is basically correct.

#include <stdio.h>

int main(void)
{
    float amount_quarts, amount_grams;
    float amount_molecules;

    printf("Please enter an amount of water in quarts:\n");
    scanf("%f", &amount_quarts);
    amount_grams = 950 * amount_quarts;
    amount_molecules = amount_grams / 3.0e-23;
    printf("The number of water molecules in %f quarts is %e molecules.\n", amount_quarts, amount_molecules);

    return 0;
}

However, when I declare the variable amount_molecules as long double, the result I end up with is completely wrong.

#include <stdio.h>

int main(void)
{
    float amount_quarts, amount_grams;
    long double amount_molecules;

    printf("Please enter an amount of water in quarts:\n");
    scanf("%f", &amount_quarts);
    amount_grams = 950 * amount_quarts;
    amount_molecules = amount_grams / 3.0e-23;
    printf("The number of water molecules in %f quarts is %Lf molecules.\n", amount_quarts, amount_molecules);

    return 0;
}

I compiled and ran it repeatedly on both CLion and Visual Studio Code, and the results were consistent. That is, when declaring variables with float and double, the calculation results are basically correct. However, when declaring variables with long double, the calculation results are completely wrong.

I think this should be a precision problem when calculating floating point numbers, but I don't understand why? And I also want to understand how I can avoid making this mistake.

like image 248
Cyprinoid_Y Avatar asked Jan 22 '26 22:01

Cyprinoid_Y


1 Answers

Beware casual type conversions

amount_molecules = amount_grams / 3.0e-23; is a float/double division and the double quotient is assigned to a long double.

To fully utilize long double math, use amount_molecules = amount_grams / 3.0e-23L; (note the L).

Even better, just use 1 floating point type, like long double throughout.

Code may have other issues too

OP comments "the long double type calculates to 1.173035e-312. Replacing %Le with %Lf, the calculation result becomes 0.000000 directly."

This hints that the library is having trouble with "%Lf".

// Instead of 
printf("The number of water molecules in %f quarts is %Lf molecules.\n", 
    amount_quarts, amount_molecules);
// Try
printf("The number of water molecules in %f quarts is %f molecules.\n", 
    amount_quarts, (double) amount_molecules);

to narrow the problem.

like image 184
chux - Reinstate Monica Avatar answered Jan 24 '26 17:01

chux - Reinstate Monica



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!