はじめに
この実験では、C 言語で浮動小数点数演算を行う方法を学びます。まず、float 型と double 型の変数を宣言し、その後それらに対して様々な算術演算を行い、最後に適切な精度で結果を出力します。この実験では、C プログラミングにおいて小数を扱い、正確な数学的計算を行うための基本的なスキルをカバーしています。
最初のステップでは、float 型と double 型のデータ型を使用して浮動小数点数の変数を宣言する方法と、希望する精度で値を出力する方法を示します。2 番目のステップでは、浮動小数点数に対する加算、減算、乗算、除算などの算術演算を行うことに焦点を当てます。
浮動小数点数型(float または double)の変数を宣言する
このステップでは、C 言語で float 型と double 型のデータ型を使用して浮動小数点数の変数を宣言する方法を学びます。浮動小数点数型の変数は、小数を格納し、正確な数学的計算を行うために不可欠です。
まず、変数宣言をデモンストレーションするために新しい C ファイルを作成しましょう。
cd ~/project
nano floating_variables.c
次に、以下のコードをファイルに追加します。
#include <stdio.h>
int main() {
// Declaring float variables
float temperature = 98.6;
float price = 19.99;
// Declaring double variables
double pi = 3.14159265359;
double large_number = 1234567890.123456789;
// Printing the variables
printf("Temperature (float): %.2f\n", temperature);
printf("Price (float): %.2f\n", price);
printf("Pi (double): %.5f\n", pi);
printf("Large Number (double): %.9f\n", large_number);
return 0;
}
プログラムをコンパイルして実行します。
gcc floating_variables.c -o floating_variables
./floating_variables
出力例:
Temperature (float): 98.60
Price (float): 19.99
Pi (double): 3.14159
Large Number (double): 1234567890.123456789
コードを解説しましょう。
floatは単精度浮動小数点数(通常 4 バイト)に使用されます。doubleは倍精度浮動小数点数(通常 8 バイト)に使用されます。%.2fと%.5fの書式指定子は、出力時の小数点以下の精度を制御します。
浮動小数点数の算術演算を行う
このステップでは、C 言語で浮動小数点数に対して加算、減算、乗算、除算などの様々な算術演算を行う方法を学びます。
浮動小数点数の算術演算をデモンストレーションするために、前のファイルを修正しましょう。
cd ~/project
nano floating_arithmetic.c
以下のコードをファイルに追加します。
#include <stdio.h>
int main() {
// Declare floating-point variables
float a = 10.5;
float b = 3.2;
// Addition
float sum = a + b;
printf("Addition: %.2f + %.2f = %.2f\n", a, b, sum);
// Subtraction
float difference = a - b;
printf("Subtraction: %.2f - %.2f = %.2f\n", a, b, difference);
// Multiplication
float product = a * b;
printf("Multiplication: %.2f * %.2f = %.2f\n", a, b, product);
// Division
float quotient = a / b;
printf("Division: %.2f / %.2f = %.2f\n", a, b, quotient);
// Mixed arithmetic operations
float mixed_calc = (a + b) * (a - b) / b;
printf("Mixed Calculation: (%.2f + %.2f) * (%.2f - %.2f) / %.2f = %.2f\n",
a, b, a, b, b, mixed_calc);
return 0;
}
プログラムをコンパイルして実行します。
gcc floating_arithmetic.c -o floating_arithmetic
./floating_arithmetic
出力例:
Addition: 10.50 + 3.20 = 13.70
Subtraction: 10.50 - 3.20 = 7.30
Multiplication: 10.50 * 3.20 = 33.60
Division: 10.50 / 3.20 = 3.28
Mixed Calculation: (10.50 + 3.20) * (10.50 - 3.20) / 3.20 = 24.41
浮動小数点数の算術演算に関する要点:
- 小数点以下の精度を制御するには
%.2f書式指定子を使用します。 - 浮動小数点数の演算は標準的な数学の規則に従います。
- 浮動小数点数の計算では、潜在的な精度の制限に注意してください。
適切な精度で結果を出力する
このステップでは、C 言語の書式指定子を使用して浮動小数点数の出力精度を制御する方法を学びます。
異なる精度の書式設定を調べるために新しいファイルを作成しましょう。
cd ~/project
nano floating_precision.c
以下のコードをファイルに追加します。
#include <stdio.h>
int main() {
// Declare floating-point variables
double pi = 3.14159265358979323846;
double large_number = 1234567.89012345;
// Default printing (limited precision)
printf("Default printing:\n");
printf("Pi: %f\n", pi);
printf("Large Number: %f\n\n", large_number);
// Controlling decimal places
printf("Controlled Precision:\n");
printf("Pi with 2 decimal places: %.2f\n", pi);
printf("Pi with 5 decimal places: %.5f\n", pi);
printf("Pi with 10 decimal places: %.10f\n\n", pi);
// Scientific notation
printf("Scientific Notation:\n");
printf("Large Number (default): %e\n", large_number);
printf("Large Number (3 decimal places): %.3e\n\n", large_number);
// Width and precision combined
printf("Width and Precision:\n");
printf("Pi (width 10, 4 decimal places): %10.4f\n", pi);
printf("Large Number (width 15, 2 decimal places): %15.2f\n", large_number);
return 0;
}
プログラムをコンパイルして実行します。
gcc floating_precision.c -o floating_precision
./floating_precision
出力例:
Default printing:
Pi: 3.141593
Large Number: 1234567.890123
Controlled Precision:
Pi with 2 decimal places: 3.14
Pi with 5 decimal places: 3.14159
Pi with 10 decimal places: 3.1415926536
Scientific Notation:
Large Number (default): 1.234568e+06
Large Number (3 decimal places): 1.235e+06
Width and Precision:
Pi (width 10, 4 decimal places): 3.1416
Large Number (width 15, 2 decimal places): 1234567.89
精度の書式設定に関する要点:
%fは浮動小数点数のデフォルトの書式指定子です。.2fは小数点以下 2 桁、.5fは小数点以下 5 桁を意味します。%eまたは%Eは科学表記を示します。- 幅指定子は出力の位置合わせと書式設定に役立ちます。
まとめ
この実験では、float 型と double 型のデータ型を使用して浮動小数点数の変数を宣言する方法、およびこれらの値に対して加算、減算、乗算、除算などの算術演算を行う方法を学びました。また、書式指定子を使用して浮動小数点数を出力する際に小数点以下の精度を制御する方法も学びました。これらのスキルは、C プログラミングにおいて小数データを扱い、正確な数学的計算を行うために不可欠です。



