Take the 2-minute tour ×
Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

Hey all I asked this over at StackOverflow =) and I got a good answer but I still have no idea whats going on. I want to know how the expression got to the answer and maybe the math behind how it was reasoned.. the steps if you would be so kind. The likelyhood is I don't understand the equation.

I'm not a big maths person please be gentle :p

For all those playing along at home I'm reading Absolute Java 5th Edition by Walter Savitch (Chapter 2 Page 66).

The Expression

double d = 12345.123456789;
System.out.printf("START%12.5e END %n", d);

The Answer

START 1.23451e+04END

I understand the basic principles of this printf method's arguments for example I know the '%' represents the start of the parameters. '12' is the number of spacing, '.5' is the times the decimal point will move.... I see the decimal point has moved 4 places towards the left... can someone explain the principles of e-notation. Also how this expression came to this answer =).

As far as e-notation go's its meant to be scientific notation;

So like 5.89e-4 would mean 0.000589 (move the decimal place if minus left if not move the decimal place right).

share|improve this question
    
I believe the ".5" means "how many digits after the decimal point do you want displayed." –  angryavian Jul 11 '13 at 14:22
    
yes thanks blf that is correct =), however the more pressing matter is how the decimal point moved 4 places to the left ;p –  bennyboy Jul 11 '13 at 14:25
    
Try a few cases and see what happens! With the same value of d, use format %12.1e and %12.2e and %12.3e and %12.4e ... Then use d=0.000589 and print it again with all those formats. –  GEdgar Jul 11 '13 at 14:34
add comment

1 Answer

up vote 1 down vote accepted

Used like this, eN (much more often EN) is just a symbol for $10^N$. It is used because scientific notation is convenient for large and small numbers and it avoids the need for superscripts. Superscripts used to be much more difficult to produce than they are today, and even today they are not easy in (some) word processors.

12.5e says allocate 12 spaces to the total output (including signs, e, etc.) and put 5 behind the decimal point. In your example there is room for a leading + sign (omitted) and eleven more characters. If the format were 12.3e you would probably get 123.451e+02 You need at least seven more total characters than decimals for the leading sign, the decimal point, e, the exponent sign, and two digits of exponent.

In your example, the decimal point moves by 4 places so there are the correct number of digits before it, then the e value was chosen to give the value 4

share|improve this answer
    
Ok so there can be 12 spaces and the decimal point is men't to move 5 spaces 1234512345.6789 so I see how it would display e+04 meaning there are 4 characters not shown correct ? I still don't understand how in the result the decimal place is all the way at the start –  bennyboy Jul 11 '13 at 14:46
    
No, the e+04 means multiply the displayed value by $10^4$. So $1.23451e+04=123.451e+02=12345.1$. If there are digits that don't fit, the number is rounded, but the e+04 doesn't tell you anything about them. If the true number were $1234512345.6789$ and you asked for 12.5e format you would get $1.23451e+09$. You would get the same if the true number were $1234510000$. The computer doesn't know how many places are correct-for floating point numbers it uses a constant number of bits. –  Ross Millikan Jul 11 '13 at 15:37
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.