Comparison of a float with a value in C
来源:互联网 发布:网络智能电视机哪家强 编辑:程序博客网 时间:2024/05/24 07:20
Predict the output of following C program.
#include<stdio.h>
int
main()
{
float
x = 0.1;
if
(x == 0.1)
printf
(
"IF"
);
else
if
(x == 0.1f)
printf
(
"ELSE IF"
);
else
printf
(
"ELSE"
);
}
The output of above program is “ELSE IF” which means the expression “x == 0.1″ returns false and expression “x == 0.1f” returns true.
Let consider the of following program to understand the reason behind the above output.
#include<stdio.h>
int
main()
{
float
x = 0.1;
printf
(
"%d %d %d"
,
sizeof
(x),
sizeof
(0.1),
sizeof
(0.1f));
return
0;
}
The output of above program is “4 8 4” on a typical C compiler. It actually prints size of float, size of double and size of float.
The values used in an expression are considered as double (double precision floating point format) unless a ‘f’ is specified at the end. So the expression “x==0.1″ has a double on right side and float which are stored in a single precision floating point format on left side. In such situations float is promoted to double (seethis). The double precision format uses uses more bits for precision than single precision format.
Note that the promotion of float to double can only cause mismatch when a value (like 0.1) uses more precision bits than the bits of single precision. For example, the following C program prints “IF”.
#include<stdio.h>
int
main()
{
float
x = 0.5;
if
(x == 0.5)
printf
(
"IF"
);
else
if
(x == 0.5f)
printf
(
"ELSE IF"
);
else
printf
(
"ELSE"
);
}
Output:
IF
You can refer Floating Point Representation – Basics for representation of floating point numbers.
This article is contributed by Abhay Rathi. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
- Comparison of a float with a value in C
- Cannot yield a value in the body of a try block with a catch clause
- the minimum value of |A - a| + |B - b| + |C - c|
- 笔记-2008-An Empirical Comparison of Goodness Measures for Unsupervised CWS with a ~
- KeilMDK编译警告:warning: #514-D: pointless comparison of unsigned integer with a negative constant
- A Comparison of Push vs Pull Ajax
- A Comparison of C# and Java(转贴)
- A Technical Comparison of TTLS and PEAP
- A Comparison of Free Search Engine Software
- Read the value of a cookie in Jmeter
- IEnumerator With a return value
- Value-typed Field of a Reference-typed Argument (C#)
- THE VALUE OF A SMILE
- Finding max & sum of a tree in Haskell with map
- Descriptor Matching with Convolutional Neural Networks: a Comparison to SIFT
- minimum value in a tree
- 关于Hibernate中的int和float数据及Null value was assigned to a property of primitive type setter of
- Redefinition of ‘*** with a different type: ‘CGFloat’ (aka ‘double’) vs ‘float’错误
- 深入浅出外观模式
- ios PDF阅读和下载
- 信必优用144部Windows Phone组超炫矩阵屏幕
- C++ 学习笔记_0002_分支条件(正差值、最大最小值及排序)
- php解析js转码之后的url
- Comparison of a float with a value in C
- JSTL标签
- Java中Request获取真实ip
- 移动应用开发—信必优信息技术
- Swoole源码学习记录(五)——锁和信号(二)
- vim设置大全
- 不使用中间变量,如何交换两个数
- 位运算妙用2
- Snippet Compiler——代码段编译工具