浮動小数点数:f32とf64
小数を扱います。
「浮動小数点数:f32とf64」はCoddyKit上の無料Zig Academyレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはZig Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Zig Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Numbers with a Fraction
When you need decimals, Zig offers floating-point types. They store fractional values like 3.14 instead of whole numbers only.
f64 Is the Common Choice
The f64 type holds a double-precision float using 64 bits. It is the usual default when you want accuracy and plenty of range.
const pi: f64 = 3.14159;f32 Trades Precision for Size
An f32 uses 32 bits. It saves memory and can be faster, but it stores fewer significant digits than an f64.
const ratio: f32 = 0.5;Zig Has More Float Widths
Beyond f32 and f64, Zig also supports f16 and f128. You choose the width that balances precision against memory and speed.
Float Literals Need a Dot
A float literal includes a decimal point, like 2.0. Writing just 2 gives an integer, which is a different kind of value.
const half = 0.5;
const one = 1.0;Scientific Notation Works Too
You can write very large or small floats with an exponent. The form 1.5e3 means 1.5 times ten to the third power, or 1500.
const big: f64 = 1.5e3;Doing Float Math
The usual operators apply to floats. Division of two floats keeps the fractional part instead of throwing it away.
const result: f64 = 7.0 / 2.0; // 3.5Floats Are Approximate
Floats cannot store every decimal exactly. Tiny rounding errors mean 0.1 plus 0.2 may not equal exactly 0.3.
Compare with a Tolerance
Because of rounding, avoid testing floats for exact equality. Check that the difference is smaller than a tiny threshold instead.
const close = @abs(a - b) < 0.0001;Convert Int to Float
To turn an integer into a float you ask explicitly with @floatFromInt. Zig never mixes the two number kinds silently.
const n: i32 = 10;
const f: f64 = @floatFromInt(n);Special Float Values
Floats can also represent infinity and not-a-number. These appear from operations like dividing by zero, and the std library names them.
Quick Check
You divide one float by another and want to keep the fractional result. What is true about floating-point math in Zig?
Recap
Use f64 for general decimals and f32 to save space. Remember floats are approximate, so compare with a tolerance and cast on purpose. 🎯
よくある質問
「浮動小数点数:f32とf64」レッスンは無料ですか?
はい。「浮動小数点数:f32とf64」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Zig Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Zig Academyコースには全4レッスンが含まれています。
「浮動小数点数:f32とf64」で何を学びますか?
小数を扱います。 ブラウザで直接実行するハンズオンコードでZig Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Zig Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのZig Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。
「浮動小数点数:f32とf64」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このZig Academyレッスンでコードを書いて実行できますか?
はい。すべてのZig Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- サイズ付き整数:i32、u8、usize
- 浮動小数点数:f32とf64
- bool、void、comptime型
- 数値リテラルとアンダースコア