Go Floating Point: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(9 intermediate revisions by the same user not shown)
Line 1: Line 1:
=External=
* https://go.dev/ref/spec#Numeric_types
=Internal=
=Internal=
* [[Go_Language#Floating_Point|Go Language]]
* [[Go_Language#Floating_Point|Go Language]]


=Overview=
=Overview=
Floating-point numbers are designated by the following [[Go_Language#Pre-Declared_Types|pre-declared type identifiers]]:
* Single precision <code>float32</code> 4 bytes IEEE-754 binary floating point, approximately 6 digits of precision.
* Double precision <code>float64</code>, 15 digits of precision.
* Complex: <code>complex64</code> and <code>complex128</code>.
There is no <code>float</code>, as in the integer case, we must explicitly provide the precision.
Uninitialized variable value: <code>+0.000000e+000</code>
Uninitialized variable value: <code>+0.000000e+000</code>


Floating-point numbers are designated by the following [[Go Concepts - Lexical Structure#Pre-Declared_Identifiers|pre-declared type identifiers]]:
=Floating-Point Literals=
* Single precision <tt>float32</tt> (4 bytes IEEE-754 binary floating point) and double precision <tt>float64</tt>.
Decimals:
* Complex: <tt>complex64</tt> and <tt>complex128</tt>.
<syntaxhighlight lang='go'>
var x float = 123.45
</syntaxhighlight>
Scientific notation, with exponent in base 10:
<syntaxhighlight lang='go'>
var x float = 1.23e3
</syntaxhighlight>
=Printing Floating Point Numbers=
{{Internal|Go_Printing_to_Stdout_and_Stderr#Floating_Point_Numbers|Printing Floating Point Numbers with <tt>fmt.Printf()</tt>}}
=Type Conversion to <tt>int</tt>=
Floating point can be truncated by converting them to <code>int</code>:
<syntaxhighlight lang='go'>
f := 1.0
i := int(f)
</syntaxhighlight>


=Floating-Point Literals=
=Complex Numbers=
<syntaxhighlight lang='go'>
var z complex128 = complex(2, 3)
</syntaxhighlight>

Latest revision as of 04:02, 23 August 2023

External

Internal

Overview

Floating-point numbers are designated by the following pre-declared type identifiers:

  • Single precision float32 4 bytes IEEE-754 binary floating point, approximately 6 digits of precision.
  • Double precision float64, 15 digits of precision.
  • Complex: complex64 and complex128.

There is no float, as in the integer case, we must explicitly provide the precision.

Uninitialized variable value: +0.000000e+000

Floating-Point Literals

Decimals:

var x float = 123.45

Scientific notation, with exponent in base 10:

var x float = 1.23e3

Printing Floating Point Numbers

Printing Floating Point Numbers with fmt.Printf()

Type Conversion to int

Floating point can be truncated by converting them to int:

f := 1.0
i := int(f)

Complex Numbers

var z complex128 = complex(2, 3)