So far, we have been using the term "numerical value."
There are two types of numbers handled in C.
It is
integers and real numbers.
Integers are numbers that include natural numbers, zero, and negative numbers.
"For example, numbers like 1, -1, 0, 5, 8, 7,..."
In short, we call the numbers we normally use, ordinary numbers, integers.
Real numbers are numbers that include integers and numbers with decimal values.
"For example, numbers like 1.0, 5.2, -9.687, 3.14159, and so on,"
In short, numbers that include decimals are called real numbers.
Also, integers can be represented using three different notations.
It is divided into three types: decimal, octal, and hexadecimal.
書式 |
進数 |
digit |
decimal number |
0digit |
octal |
0xdigit |
hexadecimal |
Numbers without leading zeros are treated as decimal numbers.
For example, 100, 25, 68, 71, and 19023 are decimal numbers.
Numbers starting with a
zero are treated as octal.
"For example, 0152, 027, 0756, and 030303 are octal numbers."
Commonly, 0152 and 152 seem like the same number.
In C, be aware that 0152 is interpreted as an octal number (106 in decimal).
For similar reasons, the number 089 would result in an error in C.
Because octal doesn't use digits like 8 or 9.
In practice, octal is rarely used, so it's probably best not to prefix it with a zero.
By the way, if it's set to 0, it's also 0 in octal, so there's no problem.
Surprisingly,
hexadecimal is frequently used, and it's represented by prefixing it with 0x.
For example, 0xFF, 0xA7, 0x912C, and 0xABCD are hexadecimal numbers.
In C, certain numeric values are used to represent specific meanings, such as character encodings.
Besides being used conventionally in hexadecimal,
Hexadecimal notation is frequently used in operations such as bitwise operations.
Of course, beginners probably won't be using it for quite some time.
Please note that we can only use decimal notation for representing real numbers.
There are various ways to handle real numbers in computers.
In C, a method called
floating-point notation is used.
For this reason, in the world of C programming, real numbers are often referred to as floating-point numbers.
【Floating-point arithmetic】
実numericsを、numericsの並び(仮数部)と、小数点の位置(指数部)で表す方法。
仮数部に、10の何乗などの値を掛け算して実数を表現する。
巨大な数から極小の数まで扱え便利だが、計算は遅い。
"In the previous chapter's calculations, the answer to 10/3 (10÷3) was incorrectly stated as 3."
I would like to calculate this as accurately as possible using real numbers.
However, since it's not a fraction, it can't be calculated perfectly accurately.Please forgive me.
この様に、コンピュータでAccurate calculationsを行うのは困難is.
実数を使用すると、必ず割りきれない数が発生する他に、
There are also errors that occur because we are calculating in binary.
"This is actually because binary cannot accurately represent numbers like 0.1."
normalであれば四捨五入してしまえば大きな問題になりませんが、
正確さが必要な銀行のコンピュータなどは、
It seems to have a mechanism for calculating as a decimal fraction.
If you want to perform calculations with real numbers, simply make the numbers real, and the calculation will automatically be done with real numbers.
The calculation itself is fine, but another thing to keep in mind is...
The output conversion specifiers used for display will also change.
"Previously, when displaying numbers, we have been using the %d specifier,"
This is a specifier for converting an integer value to a digit.
When converting real numbers to digits, you must use the
%f specifier.
Now that you understand this much, the rest is easy.
The following program is an example of adapting the program from the previous chapter to perform calculations with real numbers.
#include <stdio.h>
int main(void)
{
printf("%f\n", 10.0 + 3.0);
printf("%f\n", 10.0 - 3.0);
printf("%f\n", 10.0 * 3.0);
printf("%f\n", 10.0 / 3.0);
return 0;
}
The results of this program's execution are as follows:
13.000000
7.000000
30.000000
3.333333
It looks like the answer was calculated using real numbers.
"However, we haven't used the %(remainder) operator this time, which is perfectly natural."
Since remainders cannot be calculated with real numbers, the % operator cannot be used.