intuitiv %f output

C

Carramba

hi!

I am wondering if the is a way to get %f work intuitive, supose I
caclulate division of 2 double's and I wan't to get nice output, in case
it only one decimal I want to print only one.. and in case its 3 ... n I
want to print the requaret amount of decimals.

for example it's unnecesery to print that 3.0/2.0 is 1.50000 it's enought
to show that is 1.5, but in case the result is 1.22234243 I want to print
whole number upp to 10 (for example) decimals.

what is the smartes way to do it?


--
Thanx in advance!

;-)

______________________________________
I se the lightat the end, but every time I take a step it's get dim.
 
M

Martin Ambuhl

Carramba said:
hi!

I am wondering if the is a way to get %f work intuitive, supose I
caclulate division of 2 double's and I wan't to get nice output, in
case it only one decimal I want to print only one.. and in case its 3
... n I want to print the requaret amount of decimals.

for example it's unnecesery to print that 3.0/2.0 is 1.50000 it's
enought to show that is 1.5, but in case the result is 1.22234243 I
want to print whole number upp to 10 (for example) decimals.

what is the smartes way to do it?

#include <stdio.h>
#include <float.h>


int main(void)
{
printf("3.0 / 2/0 = %.*g\n", DBL_DIG, 3. / 2);
printf("1.22234243 = %.*g\n", DBL_DIG, 1.22234243);
return 0;
}


3.0 / 2/0 = 1.5
1.22234243 = 1.22234243
 
M

Mike Wahler

Carramba said:
hi!

I am wondering if the is a way to get %f work intuitive, supose I
caclulate division of 2 double's and I wan't to get nice output, in case
it only one decimal I want to print only one.. and in case its 3 ... n I
want to print the requaret amount of decimals.

for example it's unnecesery to print that 3.0/2.0 is 1.50000 it's enought
to show that is 1.5, but in case the result is 1.22234243 I want to print
whole number upp to 10 (for example) decimals.

what is the smartes way to do it?

#include <stdio.h>

int main()
{
double d1 = 3.0 / 2.0;
double d2 = 1.0 / 3.0;
printf("%.10g\n%.10g\n", d1, d2);
return 0;
}

Output:

1.5
0.3333333333

Note that type 'double' is only required to have
ten decimal digits of precision (but is allowed
to provide more).

-Mike
 
C

Carramba

#include <stdio.h>
#include <float.h>


int main(void)
{
printf("3.0 / 2/0 = %.*g\n", DBL_DIG, 3. / 2);
printf("1.22234243 = %.*g\n", DBL_DIG, 1.22234243);
return 0;
}


3.0 / 2/0 = 1.5
1.22234243 = 1.22234243
thanx mate! it works fine, but I run into another problem:

printf("\n%.*g %c %.*g = %.*g\n",DBL_DIG, Operand1, Operation, DBL_DIG,
Operand2, DBL_DIG, Resultat);

as u se I want to use it on several float, is here a way to asign DBL_DIG
to all of them at once?






--
Thanx in advance!

;-)

______________________________________
I se the lightat the end, but every time I take a step it's get dim.
 
M

Michael Mair

Carramba said:
thanx mate! it works fine, but I run into another problem:

printf("\n%.*g %c %.*g = %.*g\n",DBL_DIG, Operand1, Operation, DBL_DIG,
Operand2, DBL_DIG, Resultat);

as u se I want to use it on several float, is here a way to asign
DBL_DIG to all of them at once?

Nope. The format string is worked through from left to right and
arguments are taken as needed. You could do something along the
lines of

#include <stdio.h>
#include <float.h>

#define STRINGIZE(s) #s
#define XSTR(s) STRINGIZE(s)

/* FullPrecisionDouble format */
#define FPD "%."XSTR(DBL_DIG)"g"


int main (void)
{
double Operand1, Operand2, Resultat;
char Operation = '*';

Operand1 = 1.23456789012345;
Operand2 = 57;
Resultat = Operand1*Operand2;
printf("\n"FPD" %c "FPD" = "FPD"\n", Operand1, Operation,
Operand2, Resultat);
return 0;
}


Cheers
Michael
 
M

Mike Wahler

Carramba said:
thanx mate! it works fine, but I run into another problem:

printf("\n%.*g %c %.*g = %.*g\n",DBL_DIG, Operand1, Operation, DBL_DIG,
Operand2, DBL_DIG, Resultat);

as u se I want to use it on several float, is here a way to asign DBL_DIG
to all of them at once?

Not directly, using printf(), but you can wrap it in
a function and pass the common precision value as a single
parameter:

#include <stdio.h>
#include <float.h>

void show(double op1, char oper, double op2, double res, int pr)
{
printf("\n%.*g %c %.*g = %.*g\n",
pr, op1, oper, pr, op2, pr, res);
}

int main()
{
double Operand1 = 3;
double Operand2 = 2;
char Operation = '/';
double Resultat = Operand1 / Operand2;
show(Operand1, Operation, Operand2, Resultat, DBL_DIG);
return 0;
}

-Mike
 

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

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top