-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathnumutils.ipf
More file actions
executable file
·69 lines (59 loc) · 1.6 KB
/
numutils.ipf
File metadata and controls
executable file
·69 lines (59 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#ifndef NUMUTILS_INCLUDE
#define NUMUTILS_INCLUDE
Strconstant NUM_REGEX = "([+-]?(?:(?:\\d+\\.\\d+)|(?:\\d+\\.?)|(?:\\.\\d+))(?:[Ee][+-]?\\d+)?)"
Function isNaN(var)
Variable var
if (numtype(var) == 2)
return 1
endif
return 0
End
Function/S Num_complexToString(cmplx_num)
Variable/C cmplx_num
String num_out
sprintf num_out, "%.16g + %.16gi", real(cmplx_num), imag(cmplx_num)
return num_out
End
Function Num_getUnitMagnitude(num)
// Return the order of magnitude for the given number
//
// Only returns multiples of 3 (as commonly used in SI units
// prefixes), and returns a max (min) magnitude of 24 (-24).
Variable num
Variable exponent = floor(log(num))
if (exponent > 24)
exponent = 24
endif
if (exponent < -24)
exponent = -24
endif
Variable diff = mod(exponent, -3)
Variable map_key = exponent - diff
// for negative integers, push to next prefix
if (exponent < -3 && diff != 0)
map_key -= 3
endif
return map_key
End
Static Constant kTOLd = 1e-13 // double precision tolerance
Function Nums_areEqual(numA, numB, [tol])
Variable numA, numB
Variable tol
if (ParamIsDefault(tol))
tol = kTOLd
endif
Variable typeA = numtype(numA)
Variable typeB = numtype(numB)
if (typeA != typeB)
return FALSE
else
if (typeA == 1) // both values are either +/-Inf
return (numA == numB)
elseif (typeA == 2) // both values are NaN
return TRUE
else // compare values, within tolerance
return (abs(numA - numB) <= tol)
endif
endif
End
#endif