user-friendly output

U

Uno

$ indent -i3 judy1.c
$ gcc judy1.c -o out
$ ./out
The tax rate at Lowe's on Paseo is 7.001474e-02 per cent.
You owe me exactly $1.565919e+01 for the outdoor light.
$ echo "That's $15.66."
That's 5.66.
$ cat judy1.c
#include <stdio.h>
int
main ()
{
double subtotal, tax, tax_rate;
double lamp, lamp_tax, visa, difference;
tax = 6.65;
subtotal = 94.98;
lamp = 55;
visa = 74.51;

tax_rate = tax / subtotal;
lamp_tax = lamp * tax_rate;
difference = visa - lamp - lamp_tax;

printf ("The tax rate at Lowe's on Paseo is %e per cent.\n", tax_rate);
printf ("You owe me exactly $%e for the outdoor light.\n", difference);
return 0;
}

// gcc judy1.c -o out
$

What would I have had to do with this printf to make it legible for a
retired schoolteacher?
 
D

Denis McMahon

printf ("The tax rate at Lowe's on Paseo is %e per cent.\n", tax_rate);
printf ("You owe me exactly $%e for the outdoor light.\n", difference);
What would I have had to do with this printf to make it legible for a
retired schoolteacher?

Do you want to know what's wrong with your math, or what's unfriendly in
your printf format specifiers?

Calculating the tax rate as a "per cent" normally involves a factor of
100 somewhere.

eg if I pay tax of $4 on a purchase of $80, my tax % is not ($4 / $80)
(0.05%), but (($4 * 100) / $80) (5%).

Of course, if I multiply the % rate by an amount, to get the actual tax,
I need to divide by 100 somewhere to compensate, eg

5% * $110 = (5 * $110) / 100 = $440 / 100 = $5.50

or

5% * $110 = (5 / 100) * $110 = 0.05 * $110 = $5.50

Or, you can calculate tax rate as a fractional value using $4 / $80
which gives 0.05, and then use 0.05 to derive to derive the tax on other
amounts, but remembering that when you want to show the tax rate as a %
you need to convert it from it's decimal fraction by multiplying by 100.

Then there's the issue of presenting the data. You probably want to
present monetary amounts and possibly even the tax rate as a float with
a fixed number of decimal places, such as %N.Nf (where N is a decimal
integer) rather than in exponential format.

So maybe changing both "tax_rate" in the first printf to make it a true
% and not a decimal fraction, and "%e" in both printfs to a format more
commonly used with currency values and tax rates would fix both the math
errors and the presentation issues?

Rgds

Denis McMahon
 
D

Denis McMahon

so i get this:

The tax rate at Lowe's on Paseo is 0.07 per cent.
You owe me exactly $15.66 for the outdoor light.

That's not a correct expression of the tax rate.

Like the OP, you have failed to take account of the need to convert the
decimal fraction tax rate to a percentage rate when you use "per cent"
in the printf.

printf ("The tax rate at Lowe's on Paseo is %.2f per cent.\n",
(tax_rate * 100.0));

printf ("You owe me exactly $%.2f for the outdoor light.\n", difference);

%g notations are not good for currency, as they tend to drop trailing
zeroes and may convert to exponential display. Consider:

printf ("$%.2f $%.4g\n",.54,.54);
printf ("$%.2f $%.4g\n",5.38,5.38);
printf ("$%.2f $%.4g\n",53.87,53.87);
printf ("$%.2f $%.4g\n",538.7,538.7);
printf ("$%.2f $%.4g\n",5387.,5387.);
printf ("$%.2f $%.4g\n",53870.,53870.);

These give the following output:

$0.54 $0.54
$5.38 $5.38
$53.87 $53.87
$538.70 $538.7
$5387.00 $5387
$53870.00 $5.387e+04

For monetary values, I think the left hand column is better, certainly
in the 4th and 6th cases, and possibly in the 5th as well.

Of course, to make them line up properly I'd use a width specifier (in
this case I'd use 9) eg $%9.2f to give:

$ 0.54 $0.54
$ 5.38 $5.38
$ 53.87 $53.87
$ 538.70 $538.7
$ 5387.00 $5387
$ 53870.00 $5.387e+04

Now the left column certainly makes more sense as monetary values than
the right one.

Rgds

Denis McMahon
 
P

Peter Nilsson

Richard Heathfield said:
I don't think any of the replies so far has mentioned that
you got your echo statement wrong.

True. On my box it produces...

That's dev5.66.

Note sure how that's relevant to the C question though.
 
U

Uno

Denis said:
Do you want to know what's wrong with your math, or what's unfriendly in
your printf format specifiers?

Calculating the tax rate as a "per cent" normally involves a factor of
100 somewhere.

eg if I pay tax of $4 on a purchase of $80, my tax % is not ($4 / $80)
(0.05%), but (($4 * 100) / $80) (5%).

Of course, if I multiply the % rate by an amount, to get the actual tax,
I need to divide by 100 somewhere to compensate, eg

5% * $110 = (5 * $110) / 100 = $440 / 100 = $5.50

or

5% * $110 = (5 / 100) * $110 = 0.05 * $110 = $5.50

Or, you can calculate tax rate as a fractional value using $4 / $80
which gives 0.05, and then use 0.05 to derive to derive the tax on other
amounts, but remembering that when you want to show the tax rate as a %
you need to convert it from it's decimal fraction by multiplying by 100.

Then there's the issue of presenting the data. You probably want to
present monetary amounts and possibly even the tax rate as a float with
a fixed number of decimal places, such as %N.Nf (where N is a decimal
integer) rather than in exponential format.

So maybe changing both "tax_rate" in the first printf to make it a true
% and not a decimal fraction, and "%e" in both printfs to a format more
commonly used with currency values and tax rates would fix both the math
errors and the presentation issues?

Rgds

Denis McMahon

$ gcc judy1.c -o out
$ ./out
The tax rate at Lowe's on Paseo is 7.00%.
You owe me exactly $15.66 for the outdoor light.
$ cat judy1.c
#include <stdio.h>
int
main ()
{
double subtotal, tax, tax_rate;
double lamp, lamp_tax, visa, difference;
tax = 6.65;
subtotal = 94.98;
lamp = 55;
visa = 74.51;

tax_rate = tax / subtotal;
lamp_tax = lamp * tax_rate;
difference = visa - lamp - lamp_tax;

printf ("The tax rate at Lowe's on Paseo is %2.2f%%.\n", 100* tax_rate);
printf ("You owe me exactly $%2.2f for the outdoor light.\n",
difference);
return 0;
}

// gcc judy1.c -o out
$


Thx, Denis.
 
U

Uno

Peter said:
True. On my box it produces...

That's dev5.66.

Note sure how that's relevant to the C question though.

What did I change about my *nix system when I wrote
echo " .... $1 ..."
?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top