Swift Learning Notes _ data type _ floating-point type

Source: Internet
Author: User



Floating-point types



is a number that has a number of decimal points. The range represented is larger than the integer type and can store numbers larger or smaller than the int type.



3 types of representations are available:



(1) Float represents a 32-bit floating-point number, which is accurate to 6 digits after the decimal point.



(2) Double represents a 64-bit floating-point number, which is accurate to 15 digits after the decimal point.



(3) Float80 represents a 80-bit floating-point number, which can be accurate to 17 digits after the decimal point.



※float32 and Float64 are type aliases for float and double respectively


var floatv:Float = 0.12345678912345  //0.1234568
var floatv2:Float = 123.123456789 // 123.1235
var doublev:Double = 0.1234567890123456789123 //0.1234567890123457
var doublev2:Double = 1234.1234567890456789 //1234.123456789046
var float80v:Float80 = 0.12345678901234567891234 //0.123456789012345679
var float80v2:Float80 = 1234.1234567890456789 //1234.12345678904568

float80v = 0.123456789123456789123456789678 //0.123456789123456789
float80v = 3.123456789123456789123456  //3.12345678912345679
float80v = 3.1234567890123456789123456  //3.12345678901234568

var float32v:Float32 = 32.14159  //Float typealias
var float64v:Float64 = 3.141591242342342343 //Double typealias





Which floating-point type is selected depends on the range of floating-point numbers that are stored, and generally, double types are recommended.



If you declare a variable or constant without specifying a data type, the system defaults to the variable or constant of the floating-point type as the double type.


var tmpva = 3.14159//double type





The direct number of floating-point types is represented in two ways:



(1) 10 binary without prefix



(2) 16 binary prefix is 0x






There must be at least one 10-digit or 16-digit number in both sides of the decimal point.



There is also an optional exponent (exponent) that is specified in 10 floating-point numbers in uppercase or lowercase e, such as: Men = m multiplied by 10 N-Times



specified by uppercase or lowercase p in 16 floating-point numbers, such as: 0XMPN = m corresponding to the 10 binary number multiplied by 2 N-Th square


var tmpv1 = 3.14159e2 //314.159
var tmpv2 = 3.14159E-4 //0.000314159
var tmpv3 = 0xC.12p3 //96.5625
var tmpv4 = 0x0.12P3 //0.5625





As with integer types, you can add several 0 or _ to a direct number of floating-point types to provide readability.






The computer is inaccurate when storing floating-point numbers and requires extra care to compare floating-point numbers.



It is more reliable to compare whether the absolute value of the difference is less than a very small value.









If a variable or constant of two different floating-point types is to be operated on, an explicit type conversion must be performed, otherwise a compilation error is generated.


var fv1:Float = 3.14159
var dv1:Double = 3.14159

//fv1 = dv1 //cannot assign value of type Double to type Float
//dv1 = fv1 //cannot assign value of type Float to type Double
fv1 = Float(dv1)
dv1 = Double(fv1)

// error binary operator + cannot be applied to operands of type Float and Double
//fv1 + dv1
fv1 + Float(dv1)
Double(fv1) + dv1





If an integer type and a variable or constant of a floating-point type are to be operated on, an explicit type conversion must occur, otherwise a compilation error is generated.



When converting a floating-point type to an integer type, the value of the float type does not exceed the range of values represented by the integer type.



The decimal section of the value of the floating-point type is truncated.


var intv:Int = 10
var dblv:Double = 3.14159

//intv = dblv  //error cannot assign value of type Double to type Int
intv = Int(dblv) //3
//dblv = intv //erro cannot assign value of type Int to type Double
dblv = 3.14159
dblv = Double(intv)

// error binary operator + cannot be applied to operands of type Int and Double
//intv + dblv 
dblv = 3.14159
intv + Int(dblv) //6
Double(intv) + dblv ///6.14159





The direct number of an integer type can be directly assigned to a variable or constant of a floating-point type.



The direct number of the integer type and the direct numbers of the floating-point type can also be calculated, and the system will be implicitly type-converted.



However, assigning a direct number of floating-point types to a variable or constant of an integer type produces a compilation error.


dblv = 3
10 / 0.3
3.14159 + 10 //13.14159

//let cintv:Int = 4.14159 //cannot convert value of type Double to specified type Int
//var tmpint:Int = 3.14159





Swift Learning Notes _ data type _ floating-point type


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.