Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report