Floating-point numbers
導入済み
15:07
ユーザー定義データ型
単純な文字列フィールドは、意味のない値でも喜んで格納します。「車両タイプ」を求め、誰かが「バナナ」と入力しても、プログラムは異議を唱えずに受け入れます。しかし、あなたが…
英語のナレーション・英語と中国語の字幕を切り替え可能
このレッスンの内容
| English | 意味 |
|---|---|
| binary representation | A way of showing a number using only the digits zero and one. |
| repeating fraction | A fraction whose digits continue forever in a repeating pattern. |
| truncated/ˈtrʌŋkeɪtɪd/ | Describes a number shortened by removing digits without rounding. |
| register's | Belonging to a small storage location inside the processor. |
| accumulated error | The total difference from the correct value after small errors build up. |
| floating-point/ˈfləʊtɪŋ pɔɪnt/ | 浮動小数点 |
| normalisation/ˌnɔːməlaɪˈzeɪʃn/ | The process of adjusting a mantissa and exponent to use available bits efficiently. |
| approximation errors | Differences between exact values and the close values used to represent them. |
| real numbers | All numbers that can be placed on a continuous number line. |
| scientific notation | A way to write a number as a value multiplied by a power of ten. |
| fields/fiːldz/ | Parts of a database record that each hold one item of data. |
| mantissa/mænˈtɪsə/ | 尾数 |
| significant digits | Digits that give useful information about the precision of a number. |
| exponent/ekˈspəʊnənt/ | 指数 |
| two's complement/tuːz ˈkɒmplɪmənt/ | A signed binary integer representation in which a negative value is formed by inverting the positive value's bits and adding one within a fixed bit width. |
| binary fraction | A number with binary digits after a binary point. |
| bit/bɪt/ | A binary digit, taking the value zero or one. |
| denary/ˈdiːnəri/ | The base-ten number system, using the digits zero through nine. |
| sign bit/saɪn bɪt/ | The bit in a signed-number representation that indicates or contributes the number's sign. |
| normalised/ˈnɔːməlaɪzd/ | 正規化 |
| significant bit | A bit that gives useful information about the value of a number. |
| binary point | The point separating whole-number bits from fractional bits in a binary number. |
| leading zeros | Zeros placed before the first non-zero digit to give a number the required width. |
| maximises precision | Makes a stored value as exact as the available bits allow. |
| precision/prɪˈsɪʒn/ | 精度 |
| range/reɪndʒ/ | The set of values that can be represented in a given format. |
| Rounding errors/ˈraʊndɪŋ ˈerəz/ | Differences caused by replacing exact values with nearby values that can be stored. |
| fixed-point/fɪkst pɔɪnt/ | 固定小数点 |
| BCD/ˌbiː siː ˈdiː/ | A way of storing each denary digit separately using four binary digits. |
| Overflow/ˌəʊvəˈfləʊ/ | The condition when a result is too large in magnitude to be represented. |
| Underflow/ˌʌndəˈfləʊ/ | The condition when a nonzero result is too close to zero to be represented. |
| integer/ˈɪntɪdʒə/ | A whole number that may be positive, negative or zero. |
| tolerance/ˈtɒlərəns/ | The largest acceptable difference when comparing two values for near equality. |
600メートルも外れたミサイル
- 1991年2月25日、ダーランのパトリオット兵站が着来ミサイルを迎撃できず、28名の兵士が死亡しました。
- The system counted time in tenths of a second, and 0.1 has no exact binary representation: it is a repeating fraction, truncated to the register's width. Each tick lost a fraction of a microsecond.
- After a hundred hours running, the accumulated error was a third of a second, and in a third of a second the target moved 600 metres. The arithmetic was correct at every single step.
- この教訓は、浮動小数点数形式、正規化、そしてこのような事例を可能にする近似誤差です。

形式
- 非常に大きさの異なる実数を格納するために、コンピュータは科学表記法の二進形式を用い、2つのフィールドを持ちます:mantissa(有効数字)、および掛け合わせるべき2の累乗である指数。両方とも補数2で格納されます。
- mantissa を二進分数として読む:小数点の最初のビットの重みは $\tfrac12$、次は $\tfrac14$、さらに次は $\tfrac18$ です。したがって
0.1010000は $\tfrac12 + \tfrac18 = 0.625$ です。 - 指数が
00000010の場合、それは $+2$ であり、値は $0.625 \times 2^2 = 2.5$ です。

分数と2の累乗

Build a floating-point number
Flip the mantissa and exponent bits to make a value, and check whether it is normalised.
A floating-point number is stored as:
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
value = mantissa × 2^exponent — binary scientific notation, with both parts stored in two's complement.
Match each part of floating-point representation to its role.
難易度 2/5 · 140 コイン + 今日初回達成時の小ボーナス
value = mantissa × 2^exponent; normalising maximises precision; money uses fixed-point/BCD to avoid rounding.
worked example: 二進法から十進法へ
- mantissa が
10110000、指数が00000011の数があります。その十進法値を求めなさい。 - 指数は $+3$ です。mantissa は 1 で始まるため、負であり、2の補数則に従って読み取ります:
1.0110000として符号ビットが $-1$ の価値を持ち、小数点以下のビットが $\tfrac14 + \tfrac18 = 0.375$ を加えるため、mantissa は $-1 + 0.375 = -0.625$ となります。 - $\text{number} = -0.625 \times 2^3 = -5.0$.
- 最も一般的な誤りは、負の mantissa を正のように読み取ることです。他の何よりもまず最初のビットを確認してください。

A floating-point number has mantissa 10110000 and exponent 00000011. What is its denary value?
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
The mantissa starts with 1, so it is negative: $-1 + \dfrac{1}{4} + \dfrac{1}{8} = -0.625$. Times $2^{3}$ gives $-5.0$. Reading it as positive is the usual error.
worked example: 十進数から二進数への変換
- $+2.5$ を同じ8ビット mantissa、8ビット指数形式で格納します。
- 二進数では $2.5 = 10.1$ となる。2の累乗との分数として表すと: $2.5 = 0.101 \times 2^2$ 。
- したがって mantissa は
01010000、符号ビットが 0 で.101がゼロでパディングされ、指数は00000010です。 - まず正規化された形式で書くべきです。そうすれば2つのフィールドをそのまま読み取ることができます。

Written as a normalised fraction times a power of two, $2.5 = 0.101 \times 2^{n}$ (the fraction in binary). The exponent $n$ is ____.
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
$2.5$ is $10.1$ in binary; sliding the point two places left gives $0.101$, so the exponent is $2$ and the mantissa is 01010000.
正規化
- 数値が正規化されているとは、最初の有効ビットが二進点の直後にあることを指す。これにより先頭のゼロが無駄にならない。正の数であればmantissaは
0.1で始まり、負の数であれば1.0で始まる。 - 理由:これは 精度を最大化するからです。なぜなら、すべての mantissa ビットが情報を持ち、先頭のゼロではないからである。
- 正規化するには、mantissa を左にシフトして指数を同じだけ減らすか、右にシフトして増やすかします。値は変わりませんが、表現のみが変わります。

ビットをシフトし、指数を調整し、値を維持する

mantissa ビットと指数ビットのトレードオフ
- ビットの総数は固定されているため、2つのフィールドは競合します。
- mantissa ビットが多いほど 精度が高くなります:各値がより正確に格納され、丸め誤差が小さくなります。
- 指数ビットが多いほど 範囲が大きくなります:非常に大きくても非常に小さい値も表現できますが、それぞれはより不精确になります。
- 一方のフィールドから他方へのビット移動の影響を問う問題は、このトレードオフ、すなわち範囲と精度のバランスを正確に問うものです。
Normalising a floating-point number
Step through normalisation. Shifting the mantissa to remove wasted leading zeros — and adjusting the exponent to match — keeps the value the same but spends every bit on precision.
Normalising a floating-point number:
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
Normalisation shifts the mantissa so the first significant bit follows the point — every bit then carries information, and the value is unchanged.
Which are true of normalising a floating-point number? Select すべて that apply.
難易度 2/5 · 140 コイン + 今日初回達成時の小ボーナス
Normalising changes only the representation. The value is unchanged; that is what adjusting the exponent guarantees.
近似とその帰結
- 多くの十進実数は正確に二進数で保存できない。$0.1$は繰り返し分数$0.000110011\ldots$であるため、切り捨てる必要がある: 保存された値は$0.1$に近いが、決して等しくない。
- 丸め誤差は多数の演算を通じて蓄積するため、
0.1 + 0.2が0.3と厳密に一致しないことや、パトリオットの時計がずれた原因となります。 - よって、2つの実数が等しいかをテストしてはいけません:
x = 0.3の代わりにABS(x - 0.3) < 1e-9を書くべきです。また、ほぼ等しい2つの値を引くと、有効数字のほとんどが失われるので注意が必要です。 - 正確な値が必要な場合、特に通貨については、固定小数点またはBCDを使用すべきです。
Match each change in the bit allocation to its effect.
難易度 2/5 · 140 コイン + 今日初回達成時の小ボーナス
The word size is fixed, so precision and range trade against each other; overflow and underflow are the exponent field running out at each end.
オーバーフローとアンダーフロー
- オーバーフローは、結果が指数の範囲に大きすぎて、全く表現できない場合に発生します。
- アンダーフローは、結果が小さすぎて、ゼロに近すぎて指数が十分に低くできず、ゼロに丸められる場合に発生します。
- どちらも 指数フィールドのサイズという属性であり、上記のトレードオフのもう一つの側面です。

0.1 cannot be stored exactly in binary (it is a repeating fraction), so 0.1 + 0.2 does not give exactly 0.3 on a computer.
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
The tiny approximation errors add up — which is why money is handled with fixed-point or BCD, not floating-point.
For exact money calculations you should use:
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
Floating-point rounding errors are unacceptable for currency; decimal fixed-point or an appropriate BCD representation can store amounts exactly to the chosen decimal scale.
見落としやすいポイント
- The mantissa is a fraction, not an integer: the first bit after the point is a half.
- 1で始まる mantissa は負であり、2の補数則に従います。まずそのビットを確認してください。
- 正規化は精度を最大化しますが、値を変えず、数を大きくもしません。
- mantissa が多いほど精度、指数が多いほど範囲です。オーバーフローは大きすぎ、アンダーフローは小さすぎます。
理解できましたか
- 浮動小数点数は$\text{mantissa} \times 2^{\text{exponent}}$を2の補数で保存し、尾数是二进制分数である
- 変換には、mantissa を分数として読み取り(1で始まる場合は2の補数)、それを2の指数倍することで行います
- 正規化とは、点の直後に最初の有効なビットがあることで、精度を最大化します;左にシフトすると指数が下がります
- more mantissa bits give precision, more exponent bits give range; binary cannot store many reals exactly, so expect rounding errors, compare with a tolerance, and use fixed-point or BCD for currency
何が問題でしたか?この内容はサイトの運営チームだけが確認します。