Using the Built-in Arithmetic Types

C++ Primer 4/e has a good advice : ”

The number of integral types in C++ can be bewildering. C++, like C, is designed to let programs get close to the hardware when necessary, and the integral types are defined to cater to the peculiarities of various kinds of hardware. Most programmers can (and should) ignore these complexities by restricting the types they actually use.

In practice, many uses of integers involve counting. For example, programs often count the number of elements in a data structure such as a vector or an array. We’ll see in Chapters 3 and 4 that the library defines a set of types to use when dealing with the size of an object. When counting such elements it is always right to use the library-defined type intended for this purpose. When counting in other circumstances, it is usually right to use an unsigned value. Doing so avoids the possibility that a value that is too large to fit results in a (seemingly) negative result.

When performing integer arithmetic, it is rarely right to use shorts. In most programs, using shorts leads to mysterious bugs when a value is assigned to a short that is bigger than the largest number it can hold. What happens depends on the machine, but typically the value “wraps around” so that a number too large to fit turns into a large negative number. For the same reason, even though char is an integral type, the char type should be used to hold characters and not for computation. The fact that char is signed on some implementations and unsigned on others makes it problematic to use it as a computational type.

On most machines, integer calculations can safely use int. Technically speaking, an int can be as small as 16 bitstoo small for most purposes. In practice, almost all general-purpose machines use 32-bits for ints, which is often the same size used for long. The difficulty in deciding whether to use int or long occurs on machines that have 32-bit ints and 64-bit longs. On such machines, the run-time cost of doing arithmetic with longs can be considerably greater than doing the same calculation using a 32-bit int. Deciding whether to use int or long requires detailed understanding of the program and the actual run-time performance cost of using long versus int.

Determining which floating-point type to use is easier: It is almost always right to use double. The loss of precision implicit in float is significant, whereas the cost of double precision calculations versus single precision is negligible. In fact, on some machines, double precision is faster than single. The precision offered by long double usually is unnecessary and often entails considerable extra run-time cost.”

And chinese translation is easy to read :『C++ 整數型別(integral types) 的數量可能讓人困惑。C++和 Cㄧ樣,被設計用來讓程式必要時能接近硬體,而其整數型別便是被定義來迎合各種硬體特性。大部分程式員可以(也應該)忽略這些複雜特性,只使用他們實際需要的型別。

現實中很多整數用於計數。例如程式通常會計算像vector或array資料結構內的元素個數。我們將在第3,4章看到程式庫定義了一組型別用以處理上述物件的大小(亦即元素的個數)。計算這些元素個數時,使用程式庫專門為此而定義的型別是對的。其他情形下計數通常應該使用unsigned數值,這可避免數值太大而產生(表面上看起來是)負直的可能性。

執行整數算術時,shorts幾乎不會是正確的選擇。在大部分程式中,如果把「超過shorts所能存放的最大值」的數值賦予一個short,會導致神秘臭蟲。實際上發生什麼事取決於機器,但通常那個值會「折回來」(wraps around),使一個過大值變成一個大負數。同樣道理,即使char是個整數相關型別,它仍應該用來存放字元而非用來計算。char在某些編譯器上是signed而其他編譯器視之為unsigned,這一事實使得用char來計算會有問題。

在大部份機器上,int可被安全地用於整數計算。嚴格說來一個int可以小至16 bits,這對任何目的來說都未免太小。但現實中幾乎所有泛用型機器的ints都有32 bits,和long相同。在這種機器上,long算術的執行期成本可能比32-bit int作相同計算的成本大很多。究竟該使用int 或 long,必須先對程式有詳細瞭解並實際比較使用long或int的執行期成本。

決定使用哪一種浮點數型別就簡單多了:使用double幾乎總是對的。float隱含的精度遺失很顯著,而倍精度和單精度計算的成本差距簡直可被忽略。事實上在某些機器,倍精度比單精度還快。程式通常不需要long double這樣的精確度,而且執行期成本通常多很多。』

原來unsigned int是這麼好用,用double最大的困擾就是很大一串數字,但是沒錯精度差很多,那還是用double 的好。

感謝你看到這裡,很快就可以離開了,但最好的獎勵行動就是按一下幫我分享或留言,感恩喔~

點我分享到Facebook

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *