الأعداد العائمة: f32 وf64
تعامَلوا مع الأعداد الكسرية
الأعداد العائمة: f32 وf64 درس مجاني في Zig Academy على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في 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/7) وفتح باقي دورة Zig Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Zig Academy 4 دروس في المجموع.
ماذا ستتعلم في «الأعداد العائمة: f32 وf64»؟
تعامَلوا مع الأعداد الكسرية تتمرن على Zig Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Zig Academy؟
لا تُشترط خبرة سابقة. Zig Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 4.
كم من الوقت يستغرق درس «الأعداد العائمة: f32 وf64»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Zig Academy هذا؟
نعم. كل درس في Zig Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- الأعداد الصحيحة محددة الحجم: i32 وu8 وusize
- الأعداد العائمة: f32 وf64
- bool وvoid وأنواع comptime
- الثوابت العددية والشرطات السفلية