diff options
| author | Cliff Click <cliffc@acm.org> | 2024-07-17 18:11:56 +0100 |
|---|---|---|
| committer | Cliff Click <cliffc@acm.org> | 2025-02-07 09:12:14 -0800 |
| commit | b5f31ede0488d7d3c5c5624df191a6ca89ae65ad (patch) | |
| tree | 999c71785ebe380190c9e5e1025bfe85343b0d5c /docs/chapter12/README.md | |
| parent | 8aa378bee34a405d19fdccb9a9250072e7b0701b (diff) | |
| download | Simple-b5f31ede0488d7d3c5c5624df191a6ca89ae65ad.tar.gz Simple-b5f31ede0488d7d3c5c5624df191a6ca89ae65ad.zip | |
Chapter 12: Floats
Co-authored-by: Dibyendu Majumdar <mobile@majumdar.org.uk>
Co-authored-by: Thalia Archibald <thalia@archibald.dev>
Co-authored-by: tannal <tannal2409@gmail.com>
Co-authored-by: hadha <helsgithub15@gmail.com>
Co-authored-by: XmiliaH <45106915+XmiliaH@users.noreply.github.com>
Co-authored-by: Tamás Hadházy <85063808+Hels15@users.noreply.github.com>
Co-authored-by: Robert Obkircher <robert.obkircher@gmail.com>
Diffstat (limited to 'docs/chapter12/README.md')
| -rw-r--r-- | docs/chapter12/README.md | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/docs/chapter12/README.md b/docs/chapter12/README.md new file mode 100644 index 00000000..8f4a2d2d --- /dev/null +++ b/docs/chapter12/README.md @@ -0,0 +1,58 @@ +# Chapter 12: Floats + +# Table of Contents + +1. [Float](#floats) + +In this chapter, we add a floating point type and values. + +You can also read [this chapter](https://github.com/SeaOfNodes/Simple/tree/linear-chapter12) in a linear Git revision history on the [linear](https://github.com/SeaOfNodes/Simple/tree/linear) branch and [compare](https://github.com/SeaOfNodes/Simple/compare/linear-chapter11...linear-chapter12) it to the previous chapter. + +## Floats + +Floating point values start with the `flt` type and compute IEEE754 64-bit arithmetic. +Integer expressions auto-widen to floats when involved in a float expression. + +```java +flt f = 0.5; +int x = 2; +f = f+x; // Auto-widen x to 2.0, result is 2.5 +f = f+true; // Since bools are just synonyms for integers 0,1 they auto-widen also +``` + +There is no corresponding way right now to round a `flt` back to an `int`: +```java +int x = 3.14; // error +``` + +Here is Newton's method to a square root: +```java +flt guess = arg; // Initial guess is just argument +while( true ) { + // Next guess from Newton's method + flt next = (arg/guess + guess)/2; + // Break if hit fixed point + if( next == guess ) break; + guess = next; +} +return guess; +``` +When run with argument 2.0, produces 1.414213562373095. + +Floating point operations have their own Nodes: + +| Node | Op | +|------------------------|----------------------| +| AddF, SubF, MulF, DivF | Basic binary FP ops | +| MinusF | Unary negate | +| EQF, LTF, LEF | FP compare operators | + +And their own section in the Type lattice: + +Within the Type Lattice, we now add the following domain: + +* <font style="background-color:aqua">Float type</font> - Float values. The same shape as the Integer values, but floats + with a TOP, BOT and constants + + + |
