1.精度和位数的区别:
float 单精度浮点 32bit,
double 双精度浮点64bit,
decimal是高精度 128bit,浮点型。
float double 是 基本类型(primitive type),decimal不是。
float 有效数字7位,范围 ±1.5 × 10E−45 to ±3.4 × 10E38
double 有效数字15/16 位,范围 ±5.0 × 10 E−324 to ±1.7 × 10E308
decimal 有效数字 28/29 位,范围 ±1.0 × 10E−28 to ±7.9 × 10E28
( E – 下接几次方)
2.性能区别:
计算机对浮点数的运算速度大大低于对整数的运算速度,对double型的运算速度低于对float的运算速度,如果在程序中大量的使用双精度类浮点数,将会占用更多的内存单元,而计算机的处理任务也会更加繁重,但是用double类型的结果相对于float会更加精确,因此在对精度 要求不是很高的情况下我们可以采用float 类型。decimal的有效位数很大,达到了28位,但是表示的数据范围却比float和double类型小。
使用的时候会对计算时的性能有影响。
3.常数写法:
float f = 12.3F; (带F)
double x=12.3; (不带就是double)
decimal d = 12.30M; (带M)
注意:浮点数运算会有精度损失问题,有精度损失时程序不会报告,要程序员自己注意。
4.单精度)float 、(双精度)double区别实例
代码:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace double与float区别2 { class Program { static void Main(string[] args) { float a = 3.0f, b = 1.0f; float c = b / a; double d = 3.0, e = 0000.0; double f = e / d; Console.WriteLine("float c={0}\ndouble f={1}", c, f); Console.ReadLine(); } } }
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/217776.html原文链接:https://javaforall.net
