Boost C++ Libraries Home Libraries People FAQ More

PrevUpHomeNext

Definitions 定義

Introduction 簡介
Types and Values 類型與值
C++ Arithmetic Types  C++的算術類型
Numeric Types 數字類型
Range and Precision 範圍和精度
Exact, Correctly Rounded and Out-Of-Range Representations 精確的、適當捨入的和超出範圍的表示法
Standard (numeric) Conversions 標準的(數字)轉換
Subranged Conversion Direction, Subtype and Supertype 子範圍轉換方向、子類型和父類型

This section provides definitions of terms used in the Numeric Conversion library.
本節提供了在 Numeric Conversion 庫中所使用的術語的定義。

As defined by the C++ Object Model (§1.7) the storage or memory on which a C++ program runs is a contiguous sequence of bytes where each byte is a contiguous sequence of bits. 
C++ 對像模型 (§1.7) 中所定義的,一個C++程序運行於其上的 存儲 或內存是指一段連續的 字節,其中每個字節都 是一段連續的二進制位。

An object is a region of storage (§1.8) and has a type (§3.9).
一個 對像 是指一塊存儲(§1.8),它具有某種類型(§3.9)。

A type is a discrete set of values.
一個 類型 是指多個值的一個離散集。

An object of type T has an object representation which is the sequence of bytes stored in the object (§3.9/4)
類型 T 的一個對像有一個 對像表示法,即保存在該對像中的一段字節(§3.9/4)

An object of type T has a value representation which is the set of bits that determine the value of an object of that type (§3.9/4). For POD types (§3.9/10), this bitset is given by the object representation, but not all the bits in the storage need to participate in the value representation (except for character types): for example, some bits might be used for padding or there may be trap-bits.
類型 T 的一個對像有一個 值表示法,即一組二進制位的集合,它決定了該類型的一個對象的(§3.9/4)。 對於 POD 類型(§3.9/10),這個位集合就是由對像表示法所給定的,但不是在存儲中的每個二進制位都需要用於值表示法(除了字符類型以外):例如,有些二進制 位可能被用於填充或者是 trap-bits。

space

The typed value that is held by an object is the value which is determined by its value representation.
由一個對像所持有的 有類型值 即是由它的值表示法所確定的值。

An abstract value (untyped) is the conceptual information that is represented in a type (i.e. the number π).
抽像值 (無類型的) 是指在類型中所表示的概念上的信息(如數字 π)。

The intrinsic value of an object is the binary value of the sequence of unsigned characters which form its object representation.
一個對象的 固有值 是指形成其對像表示法的一串無符號字符的二進制值。

space

Abstract values can be represented in a given type.
抽像值 可以在一個給定類型中進行 表示

To represent an abstract value V in a type T is to obtain a typed value v which corresponds to the abstract value V.
在類型 T表示 抽像值 V 是指,獲得一個有類型值 v, 它對應於抽像值 V.

The operation is denoted using the rep() operator, as in: v=rep(V). v is the representation of V in the type T.
這一操作用 rep() 操作符來表示,如:v=rep(V). v 是在類型 T 中的 V表示法

For example, the abstract value π can be represented in the type double as the double value M_PI and in the type int as the int value 3
例如,抽像值 π 可以在類型 double 中表示為 double value M_PI,而在類型 int 中表示為 int value 3

space

Conversely, typed values can be abstracted.
反之,有類型值 也可以被 抽像

To abstract a typed value v of type T is to obtain the abstract value V whose representation in T is v.
抽像 類型 T 的一個有類型值 v, 就是得到一個在 T 中表示 v 的抽像值 V。 

The operation is denoted using the abt() operator, as in: V=abt(v).
這一操作用 abt() 操作符來表示,如:V=abt(v).

V is the abstraction of v of type T.
V 是類型 Tv 值的 抽像

Abstraction is just an abstract operation (you can't do it); but it is defined nevertheless because it will be used to give the definitions in the rest of this document.
抽像只是一個抽像的操作(你不能執行它);但是我們仍然對它進行定義,因為它可以用於為本文檔的其餘部分給出定義。

The C++ language defines fundamental types (§3.9.1). The following subsets of the fundamental types are intended to represent numbers:
C++ 語言定義了一些 基本類型 (§3.9.1)。以下是基本類型的一個子集,它們用於表示 數 字

signed integer types (§3.9.1/2):
有符號整數類型 (§3.9.1/2):

{signed char, signed short int, signed int, signed long int} Can be used to represent general integer numbers (both negative and positive).
{signed char, signed short int, signed int, signed long int} 可用於表示普通的整數(包括負數和正數)。

unsigned integer types (§3.9.1/3):
無符號整數類型 (§3.9.1/3):

{unsigned char, unsigned short int, unsigned int, unsigned long int} Can be used to represent positive integer numbers with modulo-arithmetic.
{unsigned char, unsigned short int, unsigned int, unsigned long int} 可用於表示模算術中的正整數。

floating-point types (§3.9.1/8):
浮點類型 (§3.9.1/8):

{float,double,long double} Can be used to represent real numbers.
{float,double,long double} 可用於表示實數。

integral or integer types (§3.9.1/7):
整型或整數類型 (§3.9.1/7):

{{signed integers},{unsigned integers}, bool, char and wchar_t}

arithmetic types (§3.9.1/8):
算術類型 (§3.9.1/8):

{{integer types},{floating types}}

The integer types are required to have a binary value representation.
整數類型要求具有一個二進制的 值表示法。

Additionally, the signed/unsigned integer types of the same base type (short, int or long) are required to have the same value representation, that is:
另外,相同基本類型(short, intlong) 的有符號/無符號整數類型要求具有相同的值表示法,即:

 int i = -3 ; // suppose value representation is: 10011 (sign bit + 4 magnitude bits)
// 假設值表示法為:
10011 (符號位 + 4 數量位)
unsigned int u = i ; // u is required to have the same 10011 as its value representation.
// u 被要求具有與 10011 相同的值表示法

In other words, the integer types signed/unsigned X use the same value representation but a different interpretation of it; that is, their typed values might differ.
換言之,整數類型 signed/unsigned X 使用相同的值表示法,但有不同的解 釋;即它們的有 類型值可能不同。

Another consequence of this is that the range for signed X is always a smaller subset of the range of unsigned X, as required by §3.9.1/3.
這一要求的另一個推論是,signed X 的範圍總是 unsigned X 的範圍的一個小子集,正如 §3.9.1/3 所要求的。

[Note] Note 說明

Always remember that unsigned types, unlike signed types, have modulo-arithmetic; that is, they do not overflow. This means that:

請記住,無符號類型不像有符號類型,它具有模算術;即它們不會溢出。這意味著:

- Always be extra careful when mixing signed/unsigned types
- 在混合使用有符號/無符號類型時,一定要額外小心

- Use unsigned types only when you need modulo arithmetic or very very large numbers. Don't use unsigned types just because you intend to deal with positive values only (you can do this with signed types as well).
- 僅在你需要模算術或需要很大很大的數字時,才使用無符號類型。不要只是因為你想處理正整數而使用無符號類型(你可以用有符號類型來處理)。

This section introduces the following definitions intended to integrate arithmetic types with user-defined types which behave like numbers. Some definitions are purposely broad in order to include a vast variety of user-defined number types.
本節介紹以下定義,目的是將算術類型與行為與數字一樣的用戶自定義類型相集成。有些定義被故意放寬,以包容大量的用戶自定義數字類型。

Within this library, the term number refers to an abstract numeric value.
在本庫中,術語 數字 是指一個抽像的數字值。

A type is numeric if:
一個類型是 數字類型, 如果:

  • It is an arithmetic type, or,
    它是一個算術類型,或者,
  • It is a user-defined type which
    它是一個用戶自定義類型,滿足
    • Represents numeric abstract values (i.e. numbers).
      它表示了數字抽像值(如多位數字)。
    • Can be converted (either implicitly or explicitly) to/from at least one arithmetic type.
      它可以與至少一種算術類型相互轉換(隱式或顯式)。
    • Has range (possibly unbounded) and precision (possibly dynamic or unlimited).
      它具有 范 圍 (可能是無界的)和 精 度 (可能是動態或無限的)。
    • Provides an specialization of std::numeric_limits.
      它提供了對 std::numeric_limits 的特化。

A numeric type is signed if the abstract values it represent include negative numbers.
一個數字類型是 有符號的, 如果它所表示的抽像值包括負數。

A numeric type is unsigned if the abstract values it represent exclude negative numbers.
一個數字類型是 無符號的, 如果它所表示的抽像值不包括負數。

A numeric type is modulo if it has modulo-arithmetic (does not overflow).
一個數字類型是 帶模的, 如果它具有模算術(不會溢出)。

A numeric type is integer if the abstract values it represent are whole numbers.
一個數字類型是 整型的, 如果它所表示的抽像值是整數。

A numeric type is floating if the abstract values it represent are real numbers.
一個數字類型是 浮點型的, 如果它所表示的抽像值是實數。

An arithmetic value is the typed value of an arithmetic type
算術值 是指算術類型的有類型值。

A numeric value is the typed value of a numeric type
數字值 是指數字類型的有類型值。

These definitions simply generalize the standard notions of arithmetic types and values by introducing a superset called numeric. All arithmetic types and values are numeric types and values, but not vice versa, since user-defined numeric types are not arithmetic types.
這些定義只是通過引用一個名為 數字 的超集,對標準中的算術類型和算術值進行了泛化。所有算術類型和算術值都是數字類型和數字值,但反之不然,因為用戶自定義的數字類型不是算術類型。

The following examples clarify the differences between arithmetic and numeric types (and values):
以下例子澄清了算術類型(和算術值)和數字類型(和數字值)間的差異:

// A numeric type which is not an arithmetic type (is user-defined)
// and which is intended to represent integer numbers (i.e., an 'integer' numeric type)
// 一個不是算術類型的數字類型(用戶自定義的),它用於表示整數(即一個'整型'的數字類型)
class MyInt
{
MyInt ( long long v ) ;
long long to_builtin();
} ;
namespace std {
template<> numeric_limits<MyInt> { ... } ;
}
// A 'floating' numeric type (double) which is also an arithmetic type (built-in),
// with a float numeric value.
// 一個'浮點'的數字類型(double),同時也是算術類型(內建的),具有浮點數值。
double pi = M_PI ;
// A 'floating' numeric type with a whole numeric value.
// NOTE: numeric values are typed valued, hence, they are, for instance,
// integer or floating, despite the value itself being whole or including
// a fractional part.
// 一個具有整數值的'浮點'數字類型。註:數字值是有類型值,因此它們是整型的或浮點型的,
// 無論它的值本身是整數的還是帶有小數的。
double two = 2.0 ;
// An integer numeric type with an integer numeric value.
// 一個具有整型數字值的整型數字類型。
MyInt i(1234);

Given a number set N, some of its elements are representable in a numeric type T.
給定一個數字集合 N,它的一些元素可以在數字類型 T 中表示。

The set of representable values of type T, or numeric set of T, is a set of numeric values whose elements are the representation of some subset of N.
類型 T 可表示的值的集合,或者說 T 的數字集合,是指一個數字值的集合,其中的元素是 N 的某個子集的表示法。

For example, the interval of int values [INT_MIN,INT_MAX] is the set of representable values of type int, i.e. the int numeric set, and corresponds to the representation of the elements of the interval of abstract values [abt(INT_MIN),abt(INT_MAX)] from the integer numbers.
例如,int 值區間 [INT_MIN,INT_MAX] 就是類型 int 的可表示值的集合,也就是 int 數字集合,它對應於來自於整數抽像值區間 [abt(INT_MIN),abt(INT_MAX)] 的各元素的表示法。

Similarly, the interval of double values [-DBL_MAX,DBL_MAX] is the double numeric set, which corresponds to the subset of the real numbers from abt(-DBL_MAX) to abt(DBL_MAX).
類似地,double 值區間 [-DBL_MAX,DBL_MAX]double 數字集合,它對應於從 abt(-DBL_MAX)abt(DBL_MAX) 的實數的一個子集。

space

Let next(x) denote the lowest numeric value greater than x.
用 next(x) 表示大於 x 的最小數字值。

Let prev(x) denote the highest numeric value lower then x.
prev(x) 表示小於 x 的最大數字值。

Let v=prev(next(V)) and v=next(prev(V)) be identities that relate a numeric typed value v with a number V.
v=prev(next(V))v=next(prev(V)) 是相同的,將一個數字類型值 v 與一個數字 V 關聯起來。

An ordered pair of numeric values x,y s.t. x<y are consecutive iff next(x)==y.
一個有序的數字值對 x,y s.t. x<y連續的 當且令當 next(x)==y.

The abstract distance between consecutive numeric values is usually referred to as a Unit in the Last Place, or ulp for short. A ulp is a quantity whose abstract magnitude is relative to the numeric values it corresponds to: If the numeric set is not evenly distributed, that is, if the abstract distance between consecutive numeric values varies along the set -as is the case with the floating-point types-, the magnitude of 1ulp after the numeric value x might be (usually is) different from the magnitude of a 1ulp after the numeric value y for x!=y.
兩個連續數字值之間的抽像距離通常被稱為 最小單元, 或簡稱 ulp。ulp 是一個與相應數字值相關的抽像量:如果這個數字集合不是均勻分佈的,即在集合中的兩個連續數字值之間的抽像距離是變化的 -就像浮點類型那樣-,那麼在數字值 x 之後的1個ulp可能會(通常就是)不同於另一個數字值 y 之後的1個ulp,如果 x!=y

Since numbers are inherently ordered, a numeric set of type T is an ordered sequence of numeric values (of type T) of the form:
因為數字是有序的,所以類型 T數字集合 就是一組數字值(類型為 T)的有序排列,形如:

REP(T)={l,next(l),next(next(l)),...,prev(prev(h)),prev(h),h}

where l and h are respectively the lowest and highest values of type T, called the boundary values of type T.
其中 lh 分別為類型 T 的最小值和最大值,稱為類型 T 的邊界值。

space

A numeric set is discrete. It has a size which is the number of numeric values in the set, a width which is the abstract difference between the highest and lowest boundary values: [abt(h)-abt(l)], and a density which is the relation between its size and width: density=size/width.
數字集合是離散的。它具有一定的 大小 即該集合中的數字值的數量,還有一定的 廣度 即最大值和最小值之間的抽像距離:[abt(h)-abt(l)],還有一定的 密度 即它的大小與廣度之比:density=size/width.

The integer types have density 1, which means that there are no unrepresentable integer numbers between abt(l) and abt(h) (i.e. there are no gaps). On the other hand, floating types have density much smaller than 1, which means that there are real numbers unrepresented between consecutive floating values (i.e. there are gaps). 
整數類型的密度為1,這意味著在 abt(l)abt(h) 之間沒有不可表示的整數(即沒有間隙)。另一方面,浮點類型的密度遠小於1,這意味著在相鄰的浮點值之間存在無法表示的實數(即有一定間隙)。

space

The interval of abstract values [abt(l),abt(h)] is the range of the type T, denoted R(T).
抽像值 [abt(l),abt(h)] 的區間稱為類型 T 的範圍,記為 R(T).

A range is a set of abstract values and not a set of numeric values. In other documents, such as the C++ standard, the word range is sometimes used as synonym for numeric set, that is, as the ordered sequence of numeric values from l to h. In this document, however, a range is an abstract interval which subtends the numeric set.
範圍是指一組抽像值的集合而不是一組數字值的集合。在其它文檔中,如C++標準,單詞 range 有時被用作 numeric set 的同義詞,即從 lh 的數字值的有序排列。不過在這個文檔中,範圍指的是一個抽像區間,它包含了數字集合。

For example, the sequence [-DBL_MAX,DBL_MAX] is the numeric set of the type double, and the real interval [abt(-DBL_MAX),abt(DBL_MAX)] is its range.
例如,序列 [-DBL_MAX,DBL_MAX] 是類型 double 的數字集合,而實數區間 [abt(-DBL_MAX),abt(DBL_MAX)] 則是它的範圍。

Notice, for instance, that the range of a floating-point type is continuous unlike its numeric set.
注意,浮點類型的範圍是 連續的, 與它的數字集合不同。

This definition was chosen because:
之所以選擇這種定義,是因為:

  • (a) The discrete set of numeric values is already given by the numeric set.
    (a) 數字值的離散集已經由數字集合給出。
  • (b) Abstract intervals are easier to compare and overlap since only boundary values need to be considered.
    (b) 抽像區間更易於比較和取交集,因為只需要考慮其邊界值即可。

This definition allows for a concise definition of subranged as given in the last section.
這種定義可以很簡明地定義下一節所給出的 subranged

The width of a numeric set, as defined, is exactly equivalent to the width of a range.
按定義,數字集合的廣度與範圍的廣度精確相等。

space

The precision of a type is given by the width or density of the numeric set.
一個類型的 精度 由數字集合的廣度或密度給定。

For integer types, which have density 1, the precision is conceptually equivalent to the range and is determined by the number of bits used in the value representation: The higher the number of bits the bigger the size of the numeric set, the wider the range, and the higher the precision.
對於密度為1的整數類型,精度在概念上相當於範圍,它由值表示法所用的二進制位數量所決定:二進制位數量越多則數字集合的大小越大,範圍的廣度越廣,精度 也越高。

For floating types, which have density <<1, the precision is given not by the width of the range but by the density. In a typical implementation, the range is determined by the number of bits used in the exponent, and the precision by the number of bits used in the mantissa (giving the maximum number of significant digits that can be exactly represented). The higher the number of exponent bits the wider the range, while the higher the number of mantissa bits, the higher the precision.
對於密度<<1的浮點類型,精度不是由範圍的廣度給定,而是由密度給定。在一個典型的實現中,範圍是由用於表示指數的二進制 位數量所決定,而精度是由用於表示尾數(給出了可以精確表示的有效數字的最大位數)的二進制位數量所決定。表示指數的二進制位數量越多則範圍越廣,而尾數 的二進制位數量越多則精度越高。

Given an abstract value V and a type T with its corresponding range [abt(l),abt(h)]:
給定一個抽像角 V 和一個範圍為 [abt(l),abt(h)] 的類型 T

If V < abt(l) or V > abt(h), V is not representable (cannot be represented) in the type T, or, equivalently, it's representation in the type T is out of range, or overflows.
如果 V < abt(l)V > abt(h), 則 V 在類型 T 中是 不可表示的(不 能被表示出來),或者說,它在類型 T 中的表示是 超出範圍的, 或是 溢出的

  • If V < abt(l), the overflow is negative.
    如果 V < abt(l),則 溢出為負
  • If V > abt(h), the overflow is positive.
    如果 V > abt(h),則 溢出為正

If V >= abt(l) and V <= abt(h), V is representable (can be represented) in the type T, or, equivalently, its representation in the type T is in range, or does not overflow.
如果 V >= abt(l)V <= abt(h), 則 V& nbsp;在類型 T 中是 可表示的(可 以被表示出來),或者說它在類型 T 中的表示是 在範圍內的, 或是 未溢出的

Notice that a numeric type, such as a C++ unsigned type, can define that any V does not overflow by always representing not V itself but the abstract value U = [ V % (abt(h)+1) ], which is always in range.
注意,一個數字類型,如C++中的無符號類型,可以定義為任意 V 都不會溢出,它的表示不是 V 本身而是抽像值 U = [ V % (abt(h)+1) ],這個值總是在範圍內 的。

Given an abstract value V represented in the type T as v, the roundoff error of the representation is the abstract difference: (abt(v)-V).
給定一個在類型 T 中表示為 v 的抽像值 V,該表示法的 捨入 誤差是指抽像距離:(abt(v)-V).

Notice that a representation is an operation, hence, the roundoff error corresponds to the representation operation and not to the numeric value itself (i.e. numeric values do not have any error themselves)
注意,一個表示法是一個操作,因此捨入誤差是對應於該表示法操作而不是對應於數字值本身(即數字值本身不存在誤差)。

  • If the roundoff is 0, the representation is exact, and V is exactly representable in the type T.
    如果捨入為0,則表示是 精確的V 在類型 T 中可以精確表示。
  • If the roundoff is not 0, the representation is inexact, and V is inexactly representable in the type T.
    如果捨入不是0,則表示是 不精確的V 在類型 T 中不能精確表示。

If a representation v in a type T -either exact or inexact-, is any of the adjacents of V in that type, that is, if v==prev or v==next, the representation is faithfully rounded. If the choice between prev and next matches a given rounding direction, it is correctly rounded.
如果在類型 T 中的一個表示 v -精確或不精確的-,是該類型中的 V 的某個鄰值,即如果 v==prevv==next,則稱該表示法是如實捨入的。如果 prevnext 間的選擇符合一個給定的 捨入方向,它就是 適當捨入的

All exact representations are correctly rounded, but not all inexact representations are. In particular, C++ requires numeric conversions (described below) and the result of arithmetic operations (not covered by this document) to be correctly rounded, but batch operations propagate roundoff, thus final results are usually incorrectly rounded, that is, the numeric value r which is the computed result is neither of the adjacents of the abstract value R which is the theoretical result.
所有精確的表示都是適當捨入的,但不精確表示則不都是。特別地,C++要求數字轉換(後文描述)和算術操作的結果(不在本文討論範圍之內)都是適當捨入的,但批量操作會傳播捨入,因此最終結果通常都不是適當捨入的,即計算結果數字值 r 不是理論結果抽像值 R 的鄰值。

Because a correctly rounded representation is always one of adjacents of the abstract value being represented, the roundoff is guaranteed to be at most 1ulp.
因為一個適當捨入的表示總是被表示的抽像值的某個鄰值,所以捨入值可以保證最多為1個ulp。

The following examples summarize the given definitions. Consider:
以下例子總結了所給出的下定義。考慮以下類型:

  • A numeric type Int representing integer numbers with a numeric set: {-2,-1,0,1,2} and range: [-2,2]
    數字類型 Int 表示 數字集合{-2,-1,0,1,2}範圍 [-2,2] 的整數
  • A numeric type Cardinal representing integer numbers with a numeric set: {0,1,2,3,4,5,6,7,8,9} and range: [0,9] (no modulo-arithmetic here)
    數字類型 Cardinal 表示 數字集合{0,1,2,3,4,5,6,7,8,9}範圍[0,9] (不帶模算術) 的整數
  • A numeric type Real representing real numbers with a numeric set: {-2.0,-1.5,-1.0,-0.5,-0.0,+0.0,+0.5,+1.0,+1.5,+2.0} and range: [-2.0,+2.0]
    數字類型 Real 表示 數字集合{-2.0,-1.5,-1.0,-0.5,-0.0,+0.0,+0.5,+1.0,+1.5,+2.0}範圍[-2.0,+2.0] 的實數
  • A numeric type Whole representing real numbers with a numeric set: {-2.0,-1.0,0.0,+1.0,+2.0} and range: [-2.0,+2.0]
    如數字類型 Whole 表示 數字集合{-2.0,-1.0,0.0,+1.0,+2.0}範圍[-2.0,+2.0] 的實數

First, notice that the types Real and Whole both represent real numbers, have the same range, but different precision.
首先,請留意類型 RealWhole,它們都表示實數,具有相同的範圍,但精度不同。

  • The integer number 1 (an abstract value) can be exactly represented in any of these types.
    整數 1 (抽像值)可以在以上任一個類型中被精確表示。
  • The integer number -1 can be exactly represented in Int, Real and Whole, but cannot be represented in Cardinal, yielding negative overflow.
    整數 -1 可以在 Int, RealWhole 中被精確表示,但不能在 Cardinal 中表示,會產生負溢出。
  • The real number 1.5 can be exactly represented in Real, and inexactly represented in the other types.
    實數 1.5 可以在 Real 中精確表示,並在其它類型中不精確地表示。
  • If 1.5 is represented as either 1 or 2 in any of the types (except Real), the representation is correctly rounded.
    如果 1.5 在任一類型(除了 Real)中被表示為 12,該表示是適當捨入的。
  • If 0.5 is represented as +1.5 in the type Real, it is incorrectly rounded.
    如果 0.5 在類型 Real 中被表示為 +1.5,則是不正確捨入的。
  • (-2.0,-1.5) are the Real adjacents of any real number in the interval [-2.0,-1.5], yet there are no Real adjacents for x < -2.0, nor for x > +2.0.
    (-2.0,-1.5) 是區間 [-2.0,-1.5] 中任一實數的 Real 鄰值,但是對於 x < -2.0, 和 x > +2.0 則沒有 Real 鄰值。

The C++ language defines Standard Conversions (§4) some of which are conversions between arithmetic types.
C++語言定義了 標準轉換 (§4),其中包括算術類型間的轉換。

These are Integral promotions (§4.5), Integral conversions (§4.7), Floating point promotions (§4.6), Floating point conversions (§4.8) and Floating-integral conversions (§4.9).
包括有 整型提升 (§4.5),整型轉換 (§4.7),浮點提升 (§4.6),浮點轉換 (§4.8) 和 浮點-整型轉換 (§4.9)。

In the sequel, integral and floating point promotions are called arithmetic promotions, and these plus integral, floating-point and floating-integral conversions are called arithmetic conversions (i.e, promotions are conversions).
概括來說,整型和浮點提升被稱為 算術提升,它們再加上整型、浮點和浮點-整型轉換,被稱為 算術轉換 (即提升也是一種轉換)。

Promotions, both Integral and Floating point, are value-preserving, which means that the typed value is not changed with the conversion.
整型和浮點型的提升都是 保留原值的,即在轉換後有類型值不會被更改。

In the sequel, consider a source typed value s of type S, the source abstract value N=abt(s), a destination type T; and whenever possible, a result typed value t of type T.
概括來說,考慮一個源類型 S 的有類型值 s,源抽像值 N=abt(s),目標類型為 T;如果能夠轉換,則結果是類型為 T 的有類型值 t

Integer to integer conversions are always defined:
整數到整數的轉換總是有定義的:

  • If T is unsigned, the abstract value which is effectively represented is not N but M=[ N % ( abt(h) + 1 ) ], where h is the highest unsigned typed value of type T.
    如果 T 是無符號的,則有效表示的抽像值不是 N 而是 M=[ N % ( abt(h) + 1 ) ],其中 h 是類型 T 的最大無符號有類型值。
  • If T is signed and N is not directly representable, the result t is implementation-defined, which means that the C++ implementation is required to produce a value t even if it is totally unrelated to s.
    如果 T 是有符號的且 N 不是可直接表示的,則結果 t依實現定義的,即C++實現被要求產生一個值 t 即使它與 s 完全無關。

Floating to Floating conversions are defined only if N is representable; if it is not, the conversion has undefined behavior.
浮點數到浮點數的轉換僅當 N 是可表示時有定義;否則轉換就是 未定義行為

  • If N is exactly representable, t is required to be the exact representation.
    如果 N 是可以精確表示的,則要求 t 是精確表示。
  • If N is inexactly representable, t is required to be one of the two adjacents, with an implementation-defined choice of rounding direction; that is, the conversion is required to be correctly rounded.
    如果 N 是不可精確表示的,則要求 t 是兩個鄰值之一,捨入的方向由實現定義來選擇;即要求轉換是適當捨入的。

Floating to Integer conversions represent not N but M=trunc(N), were trunc() is to truncate: i.e. to remove the fractional part, if any.
浮點數到整數的轉換不是 N 而是 M=trunc(N),其中 trunc() 表示截斷:即去掉小數部分,如果有的話。

  • If M is not representable in T, the conversion has undefined behavior (unless T is bool, see §4.12).
    如果 MT 中不可表示,則轉換是 未定義行為 (除非 Tbool,請見 §4.12)。

Integer to Floating conversions are always defined.
整數到浮點數的轉換總是有定義的。

  • If N is exactly representable, t is required to be the exact representation.
    如果 N 是可精確表示的,則要求 t 是精確表示。
  • If N is inexactly representable, t is required to be one of the two adjacents, with an implementation-defined choice of rounding direction; that is, the conversion is required to be correctly rounded.
    如果 N 是不可精確表示的,則要求 t 是兩個鄰值之一,捨入的方向由實現定義來選擇;即要求轉換是適當捨入的。

Given a source type S and a destination type T, there is a conversion direction denoted: S->T.
給定一個源類型 S 和一個目標類型 T轉換方向 記為:S->T.

For any two ranges the following range relation can be defined: A range X can be entirely contained in a range Y, in which case it is said that X is enclosed by Y.
對於任意兩個範圍,可以定義 範圍關係 如下:範圍 X 完全包含在 範圍 Y 中,則稱 XY 包含。

Formally: R(S) is enclosed by R(T) iif (R(S) intersection R(T)) == R(S).

正式定義:R(S)R(T) 所包含當且僅當 (R(S) intersection R(T)) == R(S).

If the source type range, R(S), is not enclosed in the target type range, R(T); that is, if (R(S) & R(T)) != R(S), the conversion direction is said to be subranged, which means that R(S) is not entirely contained in R(T) and therefore there is some portion of the source range which falls outside the target range. In other words, if a conversion direction S->T is subranged, there are values in S which cannot be represented in T because they are out of range. Notice that for S->T, the adjective subranged applies to T.
如果源類型範圍 R(S),不是被目標類型範圍 R(T) 所包含;即如果 (R(S) & R(T)) != R(S),則轉換方向被稱為 子範圍的,即 R(S) 不完全包含在 R(T) 之中,因此源範圍中有一部分落在了目標範圍以外。換句話說,如果轉換方向 S->T 是子範圍的,則 S 中有些值不能在 T 中表示,因為它們超出了範圍。注意,對於 S->T,形容詞"子範圍的"是對 T 而言的。

Examples: 
例子:

Given the following numeric types all representing real numbers:
給定以下表示實數的數字類型:

  • X with numeric set {-2.0,-1.0,0.0,+1.0,+2.0} and range [-2.0,+2.0]
    X 的數字集合為 {-2.0,-1.0,0.0,+1.0,+2.0} 範圍為 [-2.0,+2.0]
  • Y with numeric set {-2.0,-1.5,-1.0,-0.5,0.0,+0.5,+1.0,+1.5,+2.0} and range [-2.0,+2.0]
    Y 的數字集合為 {-2.0,-1.5,-1.0,-0.5,0.0,+0.5,+1.0,+1.5,+2.0} 範圍為 [-2.0,+2.0]
  • Z with numeric set {-1.0,0.0,+1.0} and range [-1.0,+1.0]
    Z 的數字集合為 {-1.0,0.0,+1.0} 範圍為 [-1.0,+1.0]

For:
對於:

(a) X->Y:

R(X) & R(Y) == R(X), then X->Y is not subranged. Thus, all values of type X are representable in the type Y.
R(X) & R(Y) == R(X),則 X->Y 不是子範圍的。因此,類型 X 的所有值在類型 Y 中都可以表示。

(b) Y->X:

R(Y) & R(X) == R(Y), then Y->X is not subranged. Thus, all values of type Y are representable in the type X, but in this case, some values are inexactly representable (all the halves). (note: it is to permit this case that a range is an interval of abstract values and not an interval of typed values)
R(Y) & R(X) == R(Y),則 Y->X 不是子範圍的。因此,類型 Y 的所有值在類型 X 中都可以表示,但是有些值是非精確表示的(所有帶0.5的)。(註:可以允許這種情形,即範圍是一個抽像值的區間而不是一個有類型值的區間)

(b) X->Z:

R(X) & R(Z) != R(X), then X->Z is subranged. Thus, some values of type X are not representable in the type Z, they fall out of range (-2.0 and +2.0).
R(X) & R(Z) != R(X), 則 X->Z 是子範圍的。因此,類型 X 有些值在類型 Z 中不能表示,它們超出了 (-2.0 and +2.0) 的範圍。

It is possible that R(S) is not enclosed by R(T), while neither is R(T) enclosed by R(S); for example, UNSIG=[0,255] is not enclosed by SIG=[-128,127]; neither is SIG enclosed by UNSIG. This implies that is possible that a conversion direction is subranged both ways. This occurs when a mixture of signed/unsigned types are involved and indicates that in both directions there are values which can fall out of range.
有可能 R(S) 不被 R(T) 所包含,而 R(T) 也不被 R(S) 所包含;例如,UNSIG=[0,255] 就不被 SIG=[-128,127] 所包含;同樣 SIG 也不被 UNSIG 所包含。這意味著有可能轉換在兩個方向上都是子範圍的。在混合使用有符號/無符號類型時就會發生這種情況,它表示在兩個方向上都有一些值超出範圍。

Given the range relation (subranged or not) of a conversion direction S->T, it is possible to classify S and T as supertype and subtype: If the conversion is subranged, which means that T cannot represent all possible values of type S, S is the supertype and T the subtype; otherwise, T is the supertype and S the subtype.
給定一個轉換方向 S->T 的範圍關係(子範圍的或不是),可以將 ST 分為 父類型子類型:如果轉換是子範圍的,即 T 不能表示類型 S 的所有可能值,則 S 為父類型而 T 為子類型;否則,T 為父類型而 S 為子類型。

For example:
例如:

R(float)=[-FLT_MAX,FLT_MAX] and R(double)=[-DBL_MAX,DBL_MAX]
R(float)=[-FLT_MAX,FLT_MAX]
R(double)=[-DBL_MAX,DBL_MAX]

If FLT_MAX < DBL_MAX:
如果 FLT_MAX < DBL_MAX:

  • double->float is subranged and supertype=double, subtype=float.
    double->float 是子範圍的且 supertype=double, subtype=float.
  • float->double is not subranged and supertype=double, subtype=float.
    float->double 不是子範圍的且 supertype=double, subtype=float.

Notice that while double->float is subranged, float->double is not, which yields the same supertype,subtype for both directions.
注意,由於 double->float 是子範圍的,而 float->double 不是,所以兩個方向均得到相同的父類型和子類型。

Now consider:
現在考慮:

R(int)=[INT_MIN,INT_MAX] and R(unsigned int)=[0,UINT_MAX]
R(int)=[INT_MIN,INT_MAX]
R(unsigned int)=[0,UINT_MAX]

A C++ implementation is required to have UINT_MAX > INT_MAX (§3.9/3), so:
C++ 實現要求具有 UINT_MAX > INT_MAX (§3.9/3),所以:

  • 'int->unsigned' is subranged (negative values fall out of range) and supertype=int, subtype=unsigned.
    'int->unsigned' 是子範圍的(負值超出了範圍)且 supertype=int, subtype=unsigned.
  • 'unsigned->int' is also subranged (high positive values fall out of range) and supertype=unsigned, subtype=int.
    'unsigned->int' 也是子範圍的(高位值超出了範圍)且 supertype=unsigned, subtype=int.

In this case, the conversion is subranged in both directions and the supertype,subtype pairs are not invariant (under inversion of direction). This indicates that none of the types can represent all the values of the other.
這種情況下,兩個方向的轉換都是子範圍的,父類型、子類型對就不相同了(在相反的方向)。這表示沒有一個類型可以表示另一個類型的所有值。

When the supertype is the same for both S->T and T->S, it is effectively indicating a type which can represent all the values of the subtype. Consequently, if a conversion X->Y is not subranged, but the opposite (Y->X) is, so that the supertype is always Y, it is said that the direction X->Y is correctly rounded value preserving, meaning that all such conversions are guaranteed to produce results in range and correctly rounded (even if inexact). For example, all integer to floating conversions are correctly rounded value preserving.
如果對於 S->TT->S 都有相同的父類型,則表明父類型可以表示子類型的所有值。因此,如果轉換 X->Y 不是子範圍的,而相反的 (Y->X) 是子範圍的,則父類型總是 Y,我們稱方向 X->Y保留適當捨入值的,意思是所有此種轉換都保證產生一個在範圍之內的結果且適當捨入(雖然可能不精確)。例如,所有整數到浮點數的轉換都是保留適當捨入值的。


PrevUpHomeNext