next up previous contents
Next: 8.9 Reporting Diagnostics Up: 8. A More Modular Previous: 8.7 Snapshot Input

8.8 Snapshot Output

The code for snapshot output is similarly simple:



\begin{Code}[nbody\_sh1.C: put\_snapshot]
\small\verbatiminput{chap8/nbody_sh1.C.put_snapshot} \end{Code}

Note that the masses, positions, and velocities are all declared as const in the declaration of the function arguments. This means that this function is not allowed to change the values of those particular arguments. Being able to specify function arguments as const is a very useful C++ feature. It can help the compiler by providing extra information; it allows the compiler to flag an error if in the body of the function an attempt is made to change one of those arguments erroneously; and most importantly, it gives the human reader useful information about the intentions of the programmer.

For all these reasons, it is important to be consistent in the use of const specifications, and to always use const wherever we can. When we do this, we thereby imply that the absence of a const specifier for an argument means that we do want to affect the value of that particular argument. For example, in the previous function get_snapshot(), masses, positions, and velocities are not preceded by const. Indeed, all three arrays are being initialized in that function, and it is useful to be able to anticipate that already from looking at the argument list, either here or at the top of the file where all functions are declared.

The first line of the body of the function sets the precision for all subsequent output. It turns out that eight-byte double precision corresponds to about 16 digits of relative accuracy. If we would output less than 16 significant digits for each real variable, we would lose information. A subsequent program reading in the snapshot that we have just written out would not have access to the full information that we had before we wrote our data. On the other hand, if we would output those numbers with more than 16 digits, the extra digits would be effective garbage. While this doesn't hurt, it is a waste of space (and possibly later processing time) to go beyond 16 digits.


next up previous contents
Next: 8.9 Reporting Diagnostics Up: 8. A More Modular Previous: 8.7 Snapshot Input
Jun Makino
平成18年10月10日