Added the Float type and many fixes. (0.24.0)
This commit is contained in:
parent
95eba1eb12
commit
41fa946f1c
19 changed files with 432 additions and 69 deletions
|
|
@ -1,5 +1,16 @@
|
|||
#!/usr/bin/env fun
|
||||
|
||||
/*
|
||||
* This file is part of the Fun programming language.
|
||||
* https://hanez.org/project/fun/
|
||||
*
|
||||
* Copyright 2025 Johannes Findeisen <you@hanez.org>
|
||||
* Licensed under the terms of the Apache-2.0 license.
|
||||
* https://opensource.org/license/apache-2-0
|
||||
*
|
||||
* Added: 2025-10-04
|
||||
*/
|
||||
|
||||
boolean b = false
|
||||
print("b => " + to_string(b))
|
||||
/* Try typeof via built-in function if available */
|
||||
|
|
|
|||
|
|
@ -7,6 +7,8 @@
|
|||
* Copyright 2025 Johannes Findeisen <you@hanez.org>
|
||||
* Licensed under the terms of the Apache-2.0 license.
|
||||
* https://opensource.org/license/apache-2-0
|
||||
*
|
||||
* Added: 2025-10-04
|
||||
*/
|
||||
|
||||
// Boolean datatype usage examples (first-class booleans)
|
||||
|
|
|
|||
78
examples/floats.fun
Executable file
78
examples/floats.fun
Executable file
|
|
@ -0,0 +1,78 @@
|
|||
#!/usr/bin/env fun
|
||||
|
||||
/*
|
||||
* This file is part of the Fun programming language.
|
||||
* https://hanez.org/project/fun/
|
||||
*
|
||||
* Copyright 2025 Johannes Findeisen <you@hanez.org>
|
||||
* Licensed under the terms of the Apache-2.0 license.
|
||||
* https://opensource.org/license/apache-2-0
|
||||
*
|
||||
* Added: 2025-10-04
|
||||
*/
|
||||
|
||||
// Float type demonstration Literals: decimal and scientific notation
|
||||
float a = 1.23
|
||||
float b = 4.0
|
||||
float c = 1e2
|
||||
float d = 5E-1
|
||||
|
||||
// Arithmetic with promotion
|
||||
float s1 = a + b // 1.23 + 4.0 = 5.23
|
||||
float s2 = c - 50 // 100.0 - 50 -> 50.0
|
||||
float p = 2 * d // 2 * 0.5 -> 1.0
|
||||
number q = 7 / 2 // int division -> 3
|
||||
float r = 7 / 2.0 // float division -> 3.5
|
||||
|
||||
print("a=" + to_string(a))
|
||||
print("a: " + typeof(a))
|
||||
print("b=" + to_string(b))
|
||||
print(typeof(b))
|
||||
print("c=" + to_string(c))
|
||||
print(typeof(c))
|
||||
print("d=" + to_string(d))
|
||||
print(typeof(d))
|
||||
print("s1=" + to_string(s1))
|
||||
print(typeof(s1))
|
||||
print("s2=" + to_string(s2))
|
||||
print(typeof(s2))
|
||||
print("p=" + to_string(p))
|
||||
print(typeof(p))
|
||||
print("q=" + to_string(q))
|
||||
print(typeof(q))
|
||||
print("r=" + to_string(r))
|
||||
print(typeof(r))
|
||||
|
||||
// to_number behavior
|
||||
string s = "42.0"
|
||||
number t = to_number(s) // preserves int when exact -> 42
|
||||
print("to_number(\"42.0\")=" + to_string(t))
|
||||
print(typeof(t))
|
||||
float u = to_number("3.14")
|
||||
print("to_number(\"3.14\")=" + to_string(u))
|
||||
print(typeof(u))
|
||||
|
||||
/* Expected output:
|
||||
a=1.23
|
||||
Float
|
||||
b=4
|
||||
Float
|
||||
c=100
|
||||
Float
|
||||
d=0.5
|
||||
Float
|
||||
s1=5.2300000000000004
|
||||
Float
|
||||
s2=50
|
||||
Float
|
||||
p=1
|
||||
Float
|
||||
q=3
|
||||
Number
|
||||
r=3.5
|
||||
Float
|
||||
to_number("42.0")=42
|
||||
Number
|
||||
to_number("3.14")=3.1400000000000001
|
||||
Float
|
||||
*/
|
||||
|
|
@ -11,10 +11,10 @@
|
|||
|
||||
// Signed integer type examples with two's complement wrapping
|
||||
|
||||
sint8 s8 = -1 // should be -1 within sint8 range
|
||||
sint16 s16 = -32768 // min sint16
|
||||
sint32 s32 = -2147483648
|
||||
sint64 s64 = -9223372036854775808
|
||||
int8 s8 = -1 // should be -1 within int8 range
|
||||
int16 s16 = -32768 // min int16
|
||||
int32 s32 = -2147483648
|
||||
int64 s64 = -9223372036854775808
|
||||
|
||||
print("s8 = " + to_string(s8) + " :: " + typeof(s8))
|
||||
print("s16 = " + to_string(s16) + " :: " + typeof(s16))
|
||||
|
|
@ -36,3 +36,15 @@ print("wrap s64 -> " + to_string(s64))
|
|||
s8 = -120
|
||||
s8 = s8 - 20 // -140 -> wraps into int8 range
|
||||
print("s8 after -20 wrap -> " + to_string(s8))
|
||||
|
||||
/* Expected output:
|
||||
s8 = -1 :: Number
|
||||
s16 = -32768 :: Number
|
||||
s32 = -2147483648 :: Number
|
||||
s64 = -9223372036854775808 :: Number
|
||||
wrap s8 -> -126
|
||||
wrap s16 -> 4464
|
||||
wrap s32 -> 0
|
||||
wrap s64 -> 0
|
||||
s8 after -20 wrap -> 116
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -40,3 +40,16 @@ print("u8 after +10 wrap -> " + to_string(u8))
|
|||
// 'number' maps to 64-bit unsigned behavior
|
||||
number n = -1 // will clamp/wrap to 64-bit (implementation-defined display)
|
||||
print("number n = " + to_string(n) + " :: " + typeof(n))
|
||||
|
||||
/* Exprected result:
|
||||
u8 = 255 :: Number
|
||||
u16 = 65535 :: Number
|
||||
u32 = 4294967295 :: Number
|
||||
u64 = -1 :: Number
|
||||
wrap u8 -> 0
|
||||
wrap u16 -> 0
|
||||
wrap u32 -> 0
|
||||
wrap u64 -> 0
|
||||
u8 after +10 wrap -> 4
|
||||
number n = -1 :: Number
|
||||
*/
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue