Jump to content

C programing -> Help


Shadow

Recommended Posts

Okay, first off, I'm just a little above complete newbie concerning C. I tried to learn it and got as far as pointers. Those I could not understand, no matter which tutorial I tried. So if anyone could propose a quality one, that would be great. But that's not the reason I'm writing this.

 

I've been on-off working on this primitive program that would convert given units to given units. Length, weight, time, temperature, all of those. And a problem has arisen right at the beginning. My problem lies in converting small units, such as millimeters, to big length units, such as AU's or Light years. To even get the program to display such big numbers (as in the equivalent of one millimeter in Astronomical Units) I had to use:

 

long double amount;

I have really now idea on how precisely it works, specifically the placeholder for it; I just copied the largest variable type I found from the net :D Anyway, I have a test program where I test out a given part of the code if I need to, so I don't have to compile the whole program, especially when it's not finished. Here's the source of that program:

 

#include <stdio.h>
#include <math.h>

long double m_ft(long double meter)
{
   long double foot;
   foot=meter*3.2808399;
   return foot;
}

int main()
{
   long double value;
   printf("Enter milimeters:");
   scanf("%Lf", &value);
   printf("%3Lf milimeters is %.25Lf Astronomical Units. Wondering how far the Sun is, eh?\n", value, 6.68458134*value*pow(10,-15));
   return 0;
}

Anyway, my problem: While the program will display the value, it will be something like "1.0000000000000000 mm = 0.00000000000000000xx AU's". And what's more, I use the variable "amount" (in this program "value") in all the conversions, so I would get things like "1.00000000000000000000 meter = x.xxxx00000000000000000 feet".

 

Now, I want to get rid of the unnecessary digits. It would suffice if someone would help me get rid of the 0 where they are really not necessary, as in the underlined example above. However, if someone where willing, I could use a better solution: How can I make my program, instead of displaying all these long numbers, display something like [math]1 mm=6.68458134*10^{-15} AU's[/math]

 

I know that I'd probably have to specify how big the numbers should be for the program to start displaying them in this format, since I obviously don't want to have stuff like [math]1 mm=1*10^0 mm's[/math], so you'd have to explain all that theory to me.

 

Thanks to anyone who helps,

 

Shadow

Link to comment
Share on other sites

You don't need those long doubles. What you want is scientific notation. Use the %e (exponential) to force scientific notation or %g (general) to use whichever of exponential or fixed is "better".

Link to comment
Share on other sites

Try printf("%0.2f", num)

 

(replace the 2 in 0.2 with however many digits you wish to display)

 

He was using the %f format. That doesn't work very well with large or small numbers.

printf ("%.8f\n", 6.68458134e-15) -> 0.00000000

printf ("%.15f\n", 6.68458134e-15) -> 0.000000000000007

printf ("%.23f\n", 6.68458134e-15) -> 0.00000000000000668458134

 

Yech. Even worse,

printf ("%.23f\n", 6.68458134e+15) -> 6684581340000000.00000000000000000000000

 

 

Use the %e or %g format.

 

Use the %e format to force scientific notation output. You can specify the precision. For example, %.8e will print the number with 8 digits to the right of the decimal place.

printf ("%.8e\n", 6.68458134e-15) -> 6.68458134e-15

printf ("%.8e\n", 6.68458134) -> 6.68458134e+00

 

Use the %g format to print in the "best" format. You can specify the number of digits in the result. %.9g will print the number with 9 digits total.

printf ("%.9g\n", 6.68458134e-15) -> 6.68458134e-15

printf ("%.9g\n", 6.68458134) -> 6.68458134

Link to comment
Share on other sites

Hey, never even knew something like that existed xD Thanks ;-)

 

A follow up question though, if I just use "%e" or "%g" by itself, without adding the "x.x" part, does it still display the whole number? Because that's the problem I would run into using float. Since float is I think an 8 byte variable I think, instead of say 0.000000000000000006684 it would just show 0.000000

Link to comment
Share on other sites

Floats are 4 bytes, or 5-6 decimal digits. Doubles are 8 bytes, or 15-16 decimal digits. C support for floats was almost non-existence in its early days. This lack of support is reflected in functions like printf. Every time you pass a float to printf the compiler automatically casts the floating point value to a double.

 

Each of %f, %g, and %e have a default display precision, which differs from the storage precision. I believe it is six digits or so. That default reflects human factors more than storage (15 or 16 digits for a double) concerns. People can't make much sense out of a long, unbroken string of digits.

Link to comment
Share on other sites

Okay, one more question; I tried out the %g double. And it works when I predefine the value of the variable, but not when I use scanf(). For example:

 

int main()
{
double value;
       value=1;
printf("%g milimeters is %g Astronomical Units. Wondering how far the Sun is, eh?\n", value, 6.68458134*value*pow(10,-15));
return 0;
}

 

That works. However this:

 

int main()
{
double value;
printf("Enter milimeters:");
scanf("%g", &value);
printf("%g milimeters is %g Astronomical Units. Wondering how far the Sun is, eh?\n", value, 6.68458134*value*pow(10,-15));
return 0;

 

doesn't work. Can anyone help? Thx

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.