Fixed bug in fmtf()

This commit is contained in:
Revar Desmera 2020-03-05 02:07:35 -08:00
parent 19ed457e38
commit 9b87420c12
2 changed files with 106 additions and 82 deletions

View file

@ -92,9 +92,10 @@ function str_int(str,base=10) =
_str_int_recurse(str,base,len(str)-1);
function _str_int_recurse(str,base,i) =
let( digit = search(str[i],"0123456789abcdef"),
last_digit = digit == [] || digit[0] >= base ? (0/0) : digit[0])
i==0 ? last_digit :
let(
digit = search(str[i],"0123456789abcdef"),
last_digit = digit == [] || digit[0] >= base ? (0/0) : digit[0]
) i==0 ? last_digit :
_str_int_recurse(str,base,i-1)*base + last_digit;
// Function: str_float()
@ -162,20 +163,19 @@ function str_frac(str,mixed=true,improper=true,signed=true) =
signed && str[0]=="-" ? -str_frac(substr(str,1),mixed=mixed,improper=improper,signed=false) :
signed && str[0]=="+" ? str_frac(substr(str,1),mixed=mixed,improper=improper,signed=false) :
mixed ? (
str_find(str," ")>0 || is_undef(str_find(str,"/")) ?
str_find(str," ")>0 || is_undef(str_find(str,"/"))? (
let(whole = str_split(str,[" "]))
_str_int_recurse(whole[0],10,len(whole[0])-1) + str_frac(whole[1], mixed=false, improper=improper, signed=false)
:
str_frac(str,mixed=false, improper=improper)
)
:
) : str_frac(str,mixed=false, improper=improper)
) : (
let(split = str_split(str,"/"))
len(split)!=2 ? (0/0) :
let(numerator = _str_int_recurse(split[0],10,len(split[0])-1),
denominator = _str_int_recurse(split[1],10,len(split[1])-1))
!improper && numerator>=denominator? (0/0) :
let(
numerator = _str_int_recurse(split[0],10,len(split[0])-1),
denominator = _str_int_recurse(split[1],10,len(split[1])-1)
) !improper && numerator>=denominator? (0/0) :
denominator<0 ? (0/0) : numerator/denominator;
)
// Function: str_num()
// Usage:
@ -225,11 +225,15 @@ function str_split(str,sep,keep_nulls=true) =
function str_split_recurse(str,sep,i,result) =
i == len(sep) ? concat(result,[str]) :
let( pos = search(sep[i], str),
let(
pos = search(sep[i], str),
end = pos==[] ? len(str) : pos[0]
)
str_split_recurse(substr(str,end+1), sep, i+1,
concat(result, [substr(str,0,end)]));
str_split_recurse(
substr(str,end+1),
sep, i+1,
concat(result, [substr(str,0,end)])
);
function _remove_empty_strs(list) =
list_remove(list, search([""], list,0)[0]);
@ -288,11 +292,15 @@ function str_find(str,pattern,start=undef,last=false,all=false) =
_str_find_first(str,pattern,len(str)-len(pattern),start);
function _str_find_first(str,pattern,max_sindex,sindex) =
sindex<=max_sindex && !_str_cmp(str,sindex, pattern) ? _str_find_first(str,pattern,max_sindex,sindex+1) :
sindex<=max_sindex && !_str_cmp(str,sindex, pattern)?
_str_find_first(str,pattern,max_sindex,sindex+1) :
(sindex <= max_sindex ? sindex : undef);
function _str_find_last(str,pattern,sindex) =
sindex>=0 && !_str_cmp(str,sindex, pattern) ? _str_find_last(str,pattern,sindex-1) :
sindex>=0 && !_str_cmp(str,sindex, pattern)?
_str_find_last(str,pattern,sindex-1) :
(sindex >=0 ? sindex : undef);
function _str_find_all(str,pattern) =
pattern == "" ? list_range(len(str)) :
[for(i=[0:1:len(str)-len(pattern)]) if (_str_cmp(str,i,pattern)) i];
@ -422,18 +430,34 @@ function fmti(i,mindigits=1) =
// fmtf(PI,12); // Returns: "3.14159265359"
// fmtf([PI,-16.75],12); // Returns: "[3.14159265359, -16.75]"
function fmtf(f,sig=12) =
assert(is_num(f))
assert(is_int(sig))
assert(sig>0)
is_list(f)? str("[",str_join(sep=", ", [for (g=f) fmtf(g,sig=sig)]),"]") :
f==0? "0" :
str(f)=="nan"? "nan" :
str(f)=="inf"? "inf" :
f<0? str("-",fmtf(-f,sig=sig)) :
let(e=floor(log(f)+1e-15))
(e<-sig/2||e>=sig)? str(fmtf(f*pow(10,-e),sig=sig),"e",e) :
let(
whole=floor(f),
part=floor((f-whole)*pow(10,sig-e-1)+0.5)
e = floor(log(f)),
mv = sig - e - 1
) mv == 0? fmti(floor(f + 0.5)) :
(e<-sig/2||mv<0)? str(fmtf(f*pow(10,-e),sig=sig),"e",e) :
let(
ff = f + pow(10,-mv)*0.5,
whole = floor(ff),
part = floor((ff-whole) * pow(10,mv))
)
part>0? str(fmti(whole), str_strip_trailing(str(".",fmti(part,mindigits=sig-e-1)),"0.")) : fmti(whole);
str_join([
str(whole),
str_strip_trailing(
str_join([
".",
fmti(part, mindigits=mv)
]),
"0."
)
]);
// vim: noexpandtab tabstop=4 shiftwidth=4 softtabstop=4 nowrap

View file

@ -8,7 +8,7 @@
//////////////////////////////////////////////////////////////////////
BOSL_VERSION = [2,0,155];
BOSL_VERSION = [2,0,156];
// Section: BOSL Library Version Functions