Comparison of several number rounding methods
来源:互联网 发布:苏德战场主战场知乎 编辑:程序博客网 时间:2024/06/06 12:25
Source code:
/* floor example */#include <stdio.h> /* printf */#include <math.h> /* floor */#define ROUND(x) ((x < 0) ? (x - 0.5f) : (x + 0.5f))int main (){ printf ("---------------------------\n"); printf ( "floor of 2.3 is %d\n", (int) (2.3) ); printf ( "floor of 3.8 is %d\n", (int) (3.8) ); printf ( "floor of -2.3 is %d\n", (int) (-2.3) ); printf ( "floor of -3.8 is %d\n", (int) (-3.8) ); printf ("---------------------------\n"); printf ( "floor of 2.3 is %d\n", (int)floor (2.3) ); printf ( "floor of 3.8 is %d\n", (int)floor (3.8) ); printf ( "floor of -2.3 is %d\n", (int)floor (-2.3) ); printf ( "floor of -3.8 is %d\n", (int)floor (-3.8) ); printf ("---------------------------\n"); printf ( "floor of 2.3 is %d\n", (int)ceil (2.3) ); printf ( "floor of 3.8 is %d\n", (int)ceil (3.8) ); printf ( "floor of -2.3 is %d\n", (int)ceil (-2.3) ); printf ( "floor of -3.8 is %d\n", (int)ceil (-3.8) ); printf ("---------------------------\n"); printf ( "floor of 2.3 is %d\n", (int)ROUND (2.3) ); printf ( "floor of 3.8 is %d\n", (int)ROUND (3.8) ); printf ( "floor of -2.3 is %d\n", (int)ROUND (-2.3) ); printf ( "floor of -3.8 is %d\n", (int)ROUND (-3.8) ); return 0;}
Result:
Conclusion:
1. (int) just truncate the integer part of a float number;
2. floor and ceil rounds number downward and upward respectively.
3. By adding a 0.5 use (int) to truncate, we can get the closest integer to the original number.
0 0
- Comparison of several number rounding methods
- Comparison of various delay methods(转载)
- FOJ 2074 Number of methods
- rounding to an arbitrary number of significant digits
- Rich Comparison Methods
- Several Extension Methods for Char
- Numerical Analysis : Several Interpolation Methods
- ABAP 动态生成内表的几种方法(Several Methods of Dynamic Internal Table Creation)
- The Development and Comparison of Robust Methods for Estimating the Fundamental Matrix
- Rounding Methods(舍入模式)
- several methods for uploading files to server
- Several address of statistics
- Inkscape - rounding corners of shapes
- Comparison of programming languages
- Comparison of Organizational Structures
- Comparison of container formats
- Comparison of database tools
- Comparison of Android versions
- POJ 1182 食物链 带权并查集
- LeetCode Validate Binary Search Tree
- 关于NSTimer的一些注意事项
- 三种快排及四种优化方式
- zzuli oj 1905: 小火山的跳子游戏
- Comparison of several number rounding methods
- js判断css动画是否完成 animation,transition
- Retrofit源码分析1
- Android 图片滑动,小圆点跟着滑动
- iOS中UISearchBar(搜索框)使用总结
- leetcode 8 Implement atoi to convert a string to an integer.
- 【OVS2.5.0源码分析】enqueue action精确流表生成过程分析
- HDU 1558 Segment set 并查集 线段相交
- 1019: A+B和C比大小