2020-02-03 07:00:36 +03:00
|
|
|
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
|
2019-10-15 04:46:40 +03:00
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
module builtin
|
|
|
|
|
2020-04-26 08:06:33 +03:00
|
|
|
import strconv.ftoa
|
2020-02-26 14:14:06 +03:00
|
|
|
|
2020-04-14 19:27:30 +03:00
|
|
|
#include <float.h>
|
2020-02-26 14:14:06 +03:00
|
|
|
// ----- f64 to string functions -----
|
|
|
|
// str return a f64 as string in scientific notation, auto display digits limit
|
|
|
|
[inline]
|
2019-10-15 04:46:40 +03:00
|
|
|
pub fn (d f64) str() string {
|
2020-02-26 14:14:06 +03:00
|
|
|
return ftoa.ftoa_64(d)
|
|
|
|
}
|
|
|
|
|
2020-05-27 06:42:48 +03:00
|
|
|
[inline]
|
|
|
|
pub fn (d any_float) str() string {
|
|
|
|
x := f64(d)
|
|
|
|
abs_x := f64_abs(x)
|
|
|
|
if abs_x >= 0.01 && abs_x < 1.0e16 {
|
|
|
|
return ftoa.f64_to_str_l(x)
|
|
|
|
} else {
|
|
|
|
return ftoa.ftoa_64(x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-26 14:14:06 +03:00
|
|
|
// return a string of the input f64 in scientific notation with digit_num deciamals displayed, max 17 digits
|
|
|
|
[inline]
|
|
|
|
pub fn (x f64) strsci(digit_num int) string {
|
|
|
|
mut n_digit := digit_num
|
|
|
|
if n_digit < 1 {
|
|
|
|
n_digit = 1
|
|
|
|
} else if n_digit > 17 {
|
|
|
|
n_digit = 17
|
|
|
|
}
|
2020-04-14 19:27:30 +03:00
|
|
|
return ftoa.f64_to_str(x, n_digit)
|
2020-02-26 14:14:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// return a decimal notation of the input f64
|
|
|
|
[inline]
|
|
|
|
pub fn (x f64) strlong() string {
|
|
|
|
return ftoa.f64_to_str_l(x)
|
2019-10-15 04:46:40 +03:00
|
|
|
}
|
|
|
|
|
2020-02-26 14:14:06 +03:00
|
|
|
// ----- f32 to string functions -----
|
|
|
|
// str return a f32 as string in scientific notation, auto display digits limit
|
|
|
|
[inline]
|
2019-10-15 04:46:40 +03:00
|
|
|
pub fn (d f32) str() string {
|
2020-02-26 14:14:06 +03:00
|
|
|
return ftoa.ftoa_32(d)
|
2019-10-15 04:46:40 +03:00
|
|
|
}
|
|
|
|
|
2020-02-26 14:14:06 +03:00
|
|
|
// return a string of the input f32 in scientific notation with digit_num deciamals displayed, max 8 digits
|
|
|
|
[inline]
|
|
|
|
pub fn (x f32) strsci(digit_num int) string {
|
|
|
|
mut n_digit := digit_num
|
|
|
|
if n_digit < 1 {
|
|
|
|
n_digit = 1
|
|
|
|
} else if n_digit > 8 {
|
|
|
|
n_digit = 8
|
|
|
|
}
|
2020-04-14 19:27:30 +03:00
|
|
|
return ftoa.f32_to_str(x, n_digit)
|
2019-12-17 01:07:13 +03:00
|
|
|
}
|
|
|
|
|
2020-02-26 14:14:06 +03:00
|
|
|
// return a decimal notation of the input f32
|
|
|
|
[inline]
|
|
|
|
pub fn (x f32) strlong() string {
|
|
|
|
return ftoa.f32_to_str_l(x)
|
2019-12-17 01:07:13 +03:00
|
|
|
}
|
|
|
|
|
2020-02-26 14:14:06 +03:00
|
|
|
// ----- C functions -----
|
2020-02-16 18:13:45 +03:00
|
|
|
[inline]
|
2019-12-18 21:56:30 +03:00
|
|
|
fn f32_abs(a f32) f32 {
|
2020-04-14 19:27:30 +03:00
|
|
|
return if a < 0 {
|
|
|
|
-a
|
|
|
|
} else {
|
|
|
|
a
|
|
|
|
}
|
2019-12-18 21:56:30 +03:00
|
|
|
}
|
|
|
|
|
2020-02-16 18:13:45 +03:00
|
|
|
[inline]
|
2019-12-18 21:56:30 +03:00
|
|
|
fn f64_abs(a f64) f64 {
|
2020-04-14 19:27:30 +03:00
|
|
|
return if a < 0 {
|
|
|
|
-a
|
|
|
|
} else {
|
|
|
|
a
|
|
|
|
}
|
2019-12-18 21:56:30 +03:00
|
|
|
}
|
2019-10-15 04:46:40 +03:00
|
|
|
|
|
|
|
// compare floats using C epsilon
|
|
|
|
// ==
|
2020-02-16 18:13:45 +03:00
|
|
|
[inline]
|
2019-10-15 04:46:40 +03:00
|
|
|
pub fn (a f64) eq(b f64) bool {
|
2019-12-18 21:56:30 +03:00
|
|
|
return f64_abs(a - b) <= C.DBL_EPSILON
|
2019-10-15 04:46:40 +03:00
|
|
|
}
|
2019-12-18 21:56:30 +03:00
|
|
|
|
2020-02-16 18:13:45 +03:00
|
|
|
[inline]
|
2019-10-15 04:46:40 +03:00
|
|
|
pub fn (a f32) eq(b f32) bool {
|
2020-05-27 06:42:48 +03:00
|
|
|
return f32_abs(a - b) <= f32(C.FLT_EPSILON)
|
2019-10-15 04:46:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (a f64) eqbit(b f64) bool {
|
|
|
|
return C.DEFAULT_EQUAL(a, b)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (a f32) eqbit(b f32) bool {
|
|
|
|
return C.DEFAULT_EQUAL(a, b)
|
|
|
|
}
|
|
|
|
|
|
|
|
// !=
|
|
|
|
fn (a f64) ne(b f64) bool {
|
|
|
|
return !a.eq(b)
|
|
|
|
}
|
2019-12-18 21:56:30 +03:00
|
|
|
|
2019-10-15 04:46:40 +03:00
|
|
|
fn (a f32) ne(b f32) bool {
|
|
|
|
return !a.eq(b)
|
|
|
|
}
|
2019-12-18 21:56:30 +03:00
|
|
|
|
2019-10-15 04:46:40 +03:00
|
|
|
pub fn (a f64) nebit(b f64) bool {
|
|
|
|
return C.DEFAULT_NOT_EQUAL(a, b)
|
|
|
|
}
|
2019-12-18 21:56:30 +03:00
|
|
|
|
2019-10-15 04:46:40 +03:00
|
|
|
pub fn (a f32) nebit(b f32) bool {
|
|
|
|
return C.DEFAULT_NOT_EQUAL(a, b)
|
|
|
|
}
|
|
|
|
|
|
|
|
// a < b
|
|
|
|
fn (a f64) lt(b f64) bool {
|
|
|
|
return a.ne(b) && a.ltbit(b)
|
|
|
|
}
|
2019-12-18 21:56:30 +03:00
|
|
|
|
2019-10-15 04:46:40 +03:00
|
|
|
fn (a f32) lt(b f32) bool {
|
|
|
|
return a.ne(b) && a.ltbit(b)
|
|
|
|
}
|
2019-12-18 21:56:30 +03:00
|
|
|
|
2019-10-15 04:46:40 +03:00
|
|
|
fn (a f64) ltbit(b f64) bool {
|
|
|
|
return C.DEFAULT_LT(a, b)
|
|
|
|
}
|
2019-12-18 21:56:30 +03:00
|
|
|
|
2019-10-15 04:46:40 +03:00
|
|
|
fn (a f32) ltbit(b f32) bool {
|
|
|
|
return C.DEFAULT_LT(a, b)
|
|
|
|
}
|
|
|
|
|
|
|
|
// a <= b
|
|
|
|
fn (a f64) le(b f64) bool {
|
|
|
|
return !a.gt(b)
|
|
|
|
}
|
2019-12-18 21:56:30 +03:00
|
|
|
|
2019-10-15 04:46:40 +03:00
|
|
|
fn (a f32) le(b f32) bool {
|
|
|
|
return !a.gt(b)
|
|
|
|
}
|
2019-12-18 21:56:30 +03:00
|
|
|
|
2019-10-15 04:46:40 +03:00
|
|
|
fn (a f64) lebit(b f64) bool {
|
|
|
|
return C.DEFAULT_LE(a, b)
|
|
|
|
}
|
2019-12-18 21:56:30 +03:00
|
|
|
|
2019-10-15 04:46:40 +03:00
|
|
|
fn (a f32) lebit(b f32) bool {
|
|
|
|
return C.DEFAULT_LE(a, b)
|
|
|
|
}
|
|
|
|
|
|
|
|
// a > b
|
|
|
|
fn (a f64) gt(b f64) bool {
|
|
|
|
return a.ne(b) && a.gtbit(b)
|
|
|
|
}
|
2019-12-18 21:56:30 +03:00
|
|
|
|
2019-10-15 04:46:40 +03:00
|
|
|
fn (a f32) gt(b f32) bool {
|
|
|
|
return a.ne(b) && a.gtbit(b)
|
|
|
|
}
|
2019-12-18 21:56:30 +03:00
|
|
|
|
2019-10-15 04:46:40 +03:00
|
|
|
fn (a f64) gtbit(b f64) bool {
|
|
|
|
return C.DEFAULT_GT(a, b)
|
|
|
|
}
|
2019-12-18 21:56:30 +03:00
|
|
|
|
2019-10-15 04:46:40 +03:00
|
|
|
fn (a f32) gtbit(b f32) bool {
|
|
|
|
return C.DEFAULT_GT(a, b)
|
|
|
|
}
|
|
|
|
|
|
|
|
// a >= b
|
|
|
|
fn (a f64) ge(b f64) bool {
|
|
|
|
return !a.lt(b)
|
|
|
|
}
|
2019-12-18 21:56:30 +03:00
|
|
|
|
2019-10-15 04:46:40 +03:00
|
|
|
fn (a f32) ge(b f32) bool {
|
|
|
|
return !a.lt(b)
|
|
|
|
}
|
2019-12-18 21:56:30 +03:00
|
|
|
|
2019-10-15 04:46:40 +03:00
|
|
|
fn (a f64) gebit(b f64) bool {
|
|
|
|
return C.DEFAULT_GE(a, b)
|
|
|
|
}
|
2019-12-18 21:56:30 +03:00
|
|
|
|
2019-10-15 04:46:40 +03:00
|
|
|
fn (a f32) gebit(b f32) bool {
|
|
|
|
return C.DEFAULT_GE(a, b)
|
|
|
|
}
|