Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> การเขียนโปรแกรม C

โปรแกรม C คูณ Floating Point Numbers (จำนวนจุดลอยตัว)


Float = Floating-point ชนิดข้อมูลพื้นฐานสำหรับตัวเลขจริง มีจุดทศนิยมที่ลอยได้ เช่น 4320.0, -3.33, 0.01226

ชนิด Floating Point

CategoryTypeMinimum SizeTypical Size
floating pointfloat4 bytes4 bytes
double8 bytes8 bytes
long double8 bytes8, 12, or 16 bytes

ช่วงค่าและ Precision

SizeRangePrecision
4 bytes±1.18e-38 to ±3.4e386-9 significant digits, typically 7
8 bytes±2.23e-308 to ±1.80e30815-18 significant digits, typically 16
80-bits (typically 12 or 16 bytes)±3.36e-4932 to ±1.18e493218-21 significant digits
16 bytes±3.36e-4932 to ±1.18e493233-36 significant digits

ตัวอย่าง

Input - a=11.23 b=6.7 Output - 75.241

คำอธิบาย - ใช้ตัวแปร Float คูณกัน แล้วแสดงผล 3 ตำแหน่งทศนิยม

ตัวอย่าง

#include <stdio.h>
int main() {
    float a, b, c;
    a = 11.23; b = 6.7;
    c = (float)(a * b);
    printf("%3f", c);
    return 0;
}

ผลลัพธ์

75.241