23.07.2013 Views

Java IO.pdf - Nguyen Dang Binh

Java IO.pdf - Nguyen Dang Binh

Java IO.pdf - Nguyen Dang Binh

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

}<br />

<strong>Java</strong> I/O<br />

/**<br />

* Overrides hashCode<br />

*/<br />

public int hashCode() {<br />

return super.hashCode() * 31 + parser.getNegativePrefix().hashCode();<br />

}<br />

Aside from special cases like Inf and NaN, the big trick in formatting both longs and doubles<br />

is to separate the mantissa from the exponent. The mantissa is the set of digits that comes<br />

before the E. For example, in 6.0220943E+23 the mantissa is 6.0220943. (Technically, the<br />

mantissa is 60220943 without a decimal point, but for reasons you'll see shortly, I need to<br />

hang onto it.) By separating the mantissa from the exponent, we can format each one<br />

separately, then concatenate them together with an "E" in between. The problem is that <strong>Java</strong><br />

doesn't have any real concept of mantissa or exponent, especially in base 10. We find the<br />

exponent by taking the log10 of the number and rounding toward zero to the nearest integer.<br />

(Math.floor(Math.log(number) / Math.log(10))). Dividing the loose line number by<br />

Math.pow(10, exponent), we get a mantissa between 1 and 9.999999999.<br />

For true scientific notation, this is exactly what you want. However, this program is a little<br />

more general and allows programmers to choose to format numbers as 13.24E+12 instead of<br />

the equivalent 1.324E+13. Therefore, if the programmer sets the minimum integer digits<br />

higher than 1, we shift the mantissa to the left (multiply by 10) and subtract 1 from the<br />

exponent for each shift. In exponential notation, unlike regular decimal notation, a minimum<br />

number of integer digits greater than the available number of integer digits does not lead to<br />

insignificant zeros.<br />

Parsing is easier. All we have to do is read the exponential string, like 6.345E-1, twice. The<br />

first time you read, you get the mantissa (6.345). The parse() method automatically stops<br />

when it encounters the nonnumeric character E. Advance the parse position one space to skip<br />

past the E and read the exponent. Then multiply the mantissa by Math.pow(10, exponent),<br />

convert that into a Double object, and return it. If there's a problem parsing the exponent, then<br />

we reset parsePosition back to its original value and return null.<br />

By the way, in case you were wondering, the fourth official language of Switzerland is<br />

Romansh, also known as Rhaeto-Romanic. Romansh is spoken by about 1.5% of<br />

Switzerland's population and survives the onslaught of German/French/English/Italian by<br />

virtue of being prevalent in many isolated mountain communities in the Alps.<br />

428

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!