What does the constant E do in the c language
The documentation for floating-point constants is here.
The form 1e2 or 1E2 means 1 times ten to the power of 2, and you're perfectly correct that 2E-1 means 2 times ten to the power of -1, or 0.2.
It's based on the scientific E notation linked by Eugene.
When I run the code it prints a weird number
That's just because you used the wrong format specifier. These are documented here, and you can see you should use one of eEfFgG for doubles.
Quick question about the %e conversion specifier in c.
I understand how the conversion works, but i'm a little confused when i encountered the solutions to these two problems as to why a 0 is added in front of the exponent in our scientific notation?!
In all the math tutorials/explanations found online, the exponents are simply written as 8.392e+2, 3.02530e+1
Now, in %m.pX, i am aware that m (minimal width) is what we check last and we fill in the extra spaces.... but why did we add the 0 in front of our exponents? I know the result fufills our "m" minimal width for each problem, but if we dont add the 0 there, couldn't we have just added two spaces to the left and that would have satisfied the (minimal width), the right amount of characters we're seeking?
x = 839.21f;
printf("|%10.3e|\n");
Answer: | 8.392e+02|
Question: why isn't it | 8.392e+2|
2.
x = 30.253f;
printf("|%12.5e|\n");
Answer: | 3.02530e+01|
Question: why isn't it | 3.02530e+1| <--- with 2 spaces in front (reddit post is not showing the 2 spaces clearly)
These two problems are from the book:
C Programming: A Modern Approach
I'm currently going through the chapters, but I don't remember it having mentioned or explained the reasoning to why we're filling in 0s for exponents.
What does the constant E do in the c language
The documentation for floating-point constants is here.
The form 1e2 or 1E2 means 1 times ten to the power of 2, and you're perfectly correct that 2E-1 means 2 times ten to the power of -1, or 0.2.
It's based on the scientific E notation linked by Eugene.
When I run the code it prints a weird number
That's just because you used the wrong format specifier. These are documented here, and you can see you should use one of eEfFgG for doubles.
In C language, for double, the format specifier is %lf
So, if you use %lf then it will print n as 0.200000
And if you use %g or %G (as supported data types are float, double) then the output will be 0.2
Quick question about the %e conversion specifier in c.
I understand how the conversion works, but i'm a little confused when i encountered the solutions to these two problems as to why a 0 is added in front of the exponent in our scientific notation?!
In all the math tutorials/explanations found online, the exponents are simply written as 8.392e+2, 3.02530e+1
Now, in %m.pX, i am aware that m (minimal width) is what we check last and we fill in the extra spaces.... but why did we add the 0 in front of our exponents? I know the result fufills our "m" minimal width for each problem, but if we dont add the 0 there, couldn't we have just added two spaces to the left and that would have satisfied the (minimal width), the right amount of characters we're seeking?
x = 839.21f;
printf("|%10.3e|\n");
Answer: | 8.392e+02|
Question: why isn't it | 8.392e+2|
2.
x = 30.253f;
printf("|%12.5e|\n");
Answer: | 3.02530e+01|
Question: why isn't it | 3.02530e+1| <--- with 2 spaces in front (reddit post is not showing the 2 spaces clearly)
These two problems are from the book:
C Programming: A Modern Approach
I'm currently going through the chapters, but I don't remember it having mentioned or explained the reasoning to why we're filling in 0s for exponents.
From the 2018 C standard, clause 6.4.4.2, reworded:
- “A floating constant has a significand part that may be followed by an exponent part and a suffix that specifies its type.”
- The significand is a sequence of digits that may include a period. It represents a number in the usual way. For example, 3.4 represents 3 ones and 4 tenths.
- The exponent part is an
eorEfollowed by an optional sign and a sequence of digits, such ase13ore-4. - The “exponent indicates the power of 10 by which the significand part is to be scaled.”
- “An unsuffixed floating constant has type
double. If suffixed by the letterforF, it has typefloat. If suffixed by the letterlorL, it has typelong double.”
Thus 6.8558e-4f represents 6.8558•10−4 and produces a float value.
Note that most C implementations use a float format based on powers of 2, not 10, so they cannot represent 6.8558•10−4 exactly. In the format commonly used, IEEE-754 binary32, the closest representable value is 0.0006855800165794789791107177734375.
The e indicates scientific notation. So 6.8558e-4 is equivalent to 6.8558×10−4.
The f is a suffix that indicates a floating-point number. (Floating-point numbers are treated as double precision by default.)
Incidentally, C99 also introduced a hexadecimal notation for floating point numbers, which you can read about here: hexadecimal floating constant in C
E notation
Most calculators and many computer programs present very large and very small results in scientific notation. Because superscripted exponents like 107 cannot always be conveniently displayed, the letter E or e is often used to represent times ten raised to the power of (which would be written as "x 10b") and is followed by the value of the exponent. Note that in this usage the character e is not related to the mathematical constant e or the exponential function ex (a confusion that is less likely with capital E); and though it stands for exponent, the notation is usually referred to as (scientific) E notation or (scientific) e notation, rather than (scientific) exponential notation (though the latter also occurs). The use of this notation is not encouraged by publications.
As for your second question:
can we use this approach in other data type or we can only use it in floating points?
See the C# spec:
Real literals [the type of numeric literals that are allowed an
Ein them] are used to write values of typesfloat,double, anddecimal.
However, you have to cast or suffix the literal appropriately when assigning to anyhting other than a Double, because any literal with an e or E in it is recognized as a Double in Visual Studio. I cannot find where this behavior is specified.
float f1 = 7E1; // Compile error. Needs F suffix (7E1F)
decimal d1 = 8E2; // Compile error. Needs M suffix (8E2M)
double d2 = 9E3; // Works.
int overninethousand = (int)9E3 + 1; // Works
In general, that's exponential/scientific notation...
1.7E+3 = 1.7 x 103 = 1700
float var = 1.54334E-34;
double var2 = 1.54334E-34;
printf("\n normal:%f\n sci:%e \n or \n sci:%E \n",var,var,var);
printf("\n normal:%f\n sci:%e \n or \n sci:%E \n",var2,var2* 1.0E3 ,var2 * 1.0e3);
I don't know any OpenCL but 32-bit C floats will hold values in the range of +/- 3.4e +/- 38 (~7 digits), and doubles much more. If you want arbitrary precision arithmetic/math you may want to look into GMP or MPFR.
According to Wikipedia:
In normalized scientific notation, the exponent b is chosen so that the absolute value of a remains at least one but less than ten (1 ≤ |a| < 10). Thus 350 is written as 3.5×102. This form allows easy comparison of numbers, as the exponent b gives the number's order of magnitude. In normalized notation, the exponent b is negative for a number with absolute value between 0 and 1 (e.g. 0.5 is written as 5×10−1). The 10 and exponent are often omitted when the exponent is 0.
Normalized scientific form is the typical form of expression of large numbers in many fields, unless an unnormalised form, such as engineering notation, is desired. Normalized scientific notation is often called exponential notation—although the latter term is more general and also applies when a is not restricted to the range 1 to 10 (as in engineering notation for instance) and to bases other than 10 (as in 3.15× 220).
The first 2 in "%2.2e" is the minimum character width to print. 1.20e+03 is 8 characters which is more than 2.
e directs that the number is printed: (sign), 1 digit, '.', followed by some digits and an exponent.
The 2nd 2 in "%2.2e" is the number of digits after the decimal point to print. 6 is used if this 2nd value is not provided.
That is 1e6, not le6, and it means 1 * 10^6 or 1000000.0
It is scientific notation.
I've seen some code online and I'm trying to work out what it is doing. In particular, I've never seen "1e" convention before.
As others have mentioned, practically speaking, 1e6 is scientific notation for 10^6 which is 1000000 or better known as 1 million. But as has already been mentioned, by David, this is actually treated as a double in C and the value is actually 1000000.0.
But I feel like these answers only focus on that specific piece of the code you provided and not the whole so I wanted to provide some additional context for you since you are trying to work out what the code is doing.
For these lines:
time_t currentTime;
time(¤tTime);
time takes a pointer time_t and operates on it, presumably writing some semblance of time to it.
The next line is actually illegal because currentTime was already declared so let's make a small modification:
uint64_t convertedTime = (uint64_t)currentTime * 1e6;
This line converts time_t currentTime to an unsigned 64-bit integer then multiplies it by 1e6 or 1000000.0. This is likely being done for unit conversion. For instance, let's assume time wrote the time in microseconds (1e-6, 10^-6, or .000001) to currentTime so multiplying it by 1e6 will give you seconds. And I say that just because of what appears to be a unit conversion here not because I actually know what time did (i.e. I'm taking the code at face value here).