Get the App
SLTechnology News&Howtos  ›  Development  › 

How C++ saves fingers to locate decimals for floating point numbers

Shulou Source: shulou.com Published: 2022-06-03 17:13:58 10月04日 Update

The editor would like to share with you how C++ saves the index to the decimal point. I hope you will get something after reading this article. Let's discuss it together.

1. Background

Saving refers to positioning decimals for floating-point numbers. For example, 1.123456. To save 1 decimal place, after calling the method, the saved result is 1.1. For example, 1.98765, the result of saving 2 decimal places is 2.00.

2. Solution

A. add header file

# include # include

B, add namespaces

Using namespace std

C, add function

/ * function name: round / * function function: data precision calculation function / * function parameter: float src: number of precision to be obtained int bits: precision (0 means to retain 0 decimal places, 1 means to retain 1 decimal place 2: indicates that 2 decimal places are reserved) / * function return value: precision result / * Author: Lee / * / float round (float src, int bits)

Function realization

Float CDemo1Dlg::round (float src, int bits) {stringstream ss; ss round (f2,2)

E, attention

For example, 1.05, double is represented as 1.0499999997 in the computer, and float is represented as 1.0500000003, but it is actually equivalent to 1.05.

The round method has an exception for cases where the number of digits processed is 5, for example, 1.05, and the result of the processing may be 1.0499999997. It says float here, and you can change it to another type. If you test it a few more times, you'll understand.

After reading this article, I believe you have a certain understanding of "how C++ saves the floating point to locate decimals". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!

Tags: Decimals functions results points precision Category numbers methods articles processing middle tables digits functions parameters finished actual decimal points degrees situations data files Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi MySQL Apple vpn Microsoft