text
stringlengths
0
15.7k
source
stringlengths
6
112
set hexText to "0" & hexText
lns3.scpt
return hexPrefix & hexText
lns3.scpt
else -- format list of number
lns3.scpt
if chunkSize < 1 or chunkSize > 1024 then _support's throwInvalidParameter(chunkSize, "width", integer, "Must be an integer between 1 and 1024.") -- chunksize must be given
lns3.scpt
set {padText, maxSize} to {"", 16 ^ chunkSize - 1}
lns3.scpt
repeat chunkSize times
lns3.scpt
set padText to padText & "0"
lns3.scpt
property _list_ : theNumber's items
lns3.scpt
repeat with i from 1 to resultList's _list_'s length
lns3.scpt
set theNumber to resultList's _list_'s item i
lns3.scpt
set theInteger to theNumber as integer
lns3.scpt
if theInteger ≠ theNumber as real or theInteger < 0 or theInteger > maxSize then error number -1700
lns3.scpt
if theInteger > maxSize then
lns3.scpt
set eText to "Number is too large to represent as " & chunkSize & "-digit hexadecimal text (not between 0 and " & (16 ^ chunkSize - 1) div 1 & ")."
lns3.scpt
set eText to "Not a non-negative, non-fractional number."
lns3.scpt
_support's throwInvalidParameter(a reference to resultList's _list_'s item i, "", integer, eText)
lns3.scpt
repeat while theInteger > 0
lns3.scpt
set hexText to (item (theInteger mod 16 + 1) of "0123456789ABCDEF") & hexText
lns3.scpt
set theInteger to theInteger div 16
lns3.scpt
set resultList's _list_'s item i to (padText & hexText)'s text -chunkSize thru -1
lns3.scpt
if hasPrefix then
lns3.scpt
set hexText to "0x" & resultList's _list_
lns3.scpt
set hexText to resultList's _list_ as text
lns3.scpt
return hexText
lns3.scpt
_error("format hex", eText, eNumber, eFrom, eTo)
lns3.scpt
end format hex
lns3.scpt
to parse hex hexText width chunkSize : (0) precision loss isPrecise : (false)
lns3.scpt
set hexText to _support's asTextParameter(hexText, "")
lns3.scpt
set isPrecise to not _support's asBooleanParameter(isPrecise, "precision loss")
lns3.scpt
if chunkSize = 0 then -- read as single number
lns3.scpt
set theNumber to 0
lns3.scpt
set isNegative to hexText starts with "-"
lns3.scpt
if isNegative then set hexText to text 2 thru -1 of hexText
lns3.scpt
if hexText starts with "0x" then set hexText to text 3 thru -1 of hexText
lns3.scpt
repeat with charRef in hexText
lns3.scpt
set theNumber to theNumber * 16
lns3.scpt
set i to offset of charRef in "0123456789ABCDEF"
lns3.scpt
if i = 0 then error number -1728
lns3.scpt
set theNumber to theNumber + i - 1
lns3.scpt
on error number -1728 -- catch errors if hexText is too short or contains non-hex chars
lns3.scpt
_support's throwInvalidParameter(hexText, "", text, "Text is not a valid hexadecimal number.")
lns3.scpt
if isPrecise and (theNumber = theNumber + 1) then _support's throwInvalidParameter(hexText, "", text, "Hexadecimal text is too large to represent as a real number without losing precision.")
lns3.scpt
if isNegative then set theNumber to -theNumber
lns3.scpt
else -- read as list of numbers
lns3.scpt
if (hexText's length) mod chunkSize ≠ 0 then _support's throwInvalidParameter(hexText, "", text, "Can't split hexadecimal text exactly into " & chunkSize & "-digit chunks.")
lns3.scpt
repeat with i from 1 to hexText's length by chunkSize
lns3.scpt
repeat with charRef in hexText's text i thru (i + chunkSize - 1)
lns3.scpt
if i = 0 then _support's throwInvalidParameter(hexText, "", text, "Text is not a valid hexadecimal number.")
lns3.scpt
if isPrecise and (theNumber = theNumber + 1) then _support's throwInvalidParameter(a reference to text i thru (i + chunkSize - 1) of hexText, "", text, "Hexadecimal text is too large to convert to number without losing precision.")
lns3.scpt
set end of resultList's _list_ to theNumber
lns3.scpt
return resultList's _list_
lns3.scpt
_error("parse hex", eText, eNumber, eFrom, eTo)
lns3.scpt
end parse hex
lns3.scpt
to deg2rad x
lns3.scpt
return (x as real) * (pi / 180)
lns3.scpt
_error("radians", eText, eNumber, eFrom, eTo)
lns3.scpt
end deg2rad
lns3.scpt
to rad2deg x
lns3.scpt
return (x as real) / (pi / 180)
lns3.scpt
_error("degrees", eText, eNumber, eFrom, eTo)
lns3.scpt
end rad2deg
lns3.scpt
to abs x
lns3.scpt
set x to x as number
lns3.scpt
return -x
lns3.scpt
_error("abs", eText, eNumber, eFrom, eTo)
lns3.scpt
to cmp {n1, n2}
lns3.scpt
if (count {n1, n2} each integer) = 2 then
lns3.scpt
if n1 = n2 then return 0
lns3.scpt
set {n1, n2} to {n1 as real, n2 as real}
lns3.scpt
set dn to _isEqualDelta * n2
lns3.scpt
set dn to _isEqualDelta * n1
lns3.scpt
set dm to -dn
lns3.scpt
if dm > dn then set {dn, dm} to {dm, dn}
lns3.scpt
if d > dm and d < dn then return 0
lns3.scpt
if n1 < n2 then
lns3.scpt
return -1
lns3.scpt
return 1
lns3.scpt
_error("cmp", eText, eNumber, eFrom, eTo)
lns3.scpt
end cmp
lns3.scpt
to min theList
lns3.scpt
property _list_ : _support's asListParameter(theList, "")
lns3.scpt
if listObject's _list_'s length = 0 then _support's throwInvalidParameter(theList, "", list, "List can’t be empty.")
lns3.scpt
set theResult to (item 1 of listObject's _list_) as number
lns3.scpt
repeat with i from 1 to listObject's _list_'s length
lns3.scpt
set n to listObject's _list_'s item i as number
lns3.scpt
if n < theResult then set theResult to n
lns3.scpt
on error eText number -1700 -- couldn't coerce an item to number
lns3.scpt
_support's throwInvalidParameter(a reference to item i of theList, "", number, "List item is wrong type: " & eText) -- note: AS doesn't provide a meaningful reference when `repeat with aRef in listObject's _list_` is used (e.g. `item 2 of _list_`), so to provide useful error messages a counting loop is used and a readable reference constructed using that (e.g. `item 2 of {3, "x"}`)
lns3.scpt
_error("min", eText, eNumber, eFrom, eTo)
lns3.scpt
to max theList
lns3.scpt
if n > theResult then set theResult to n
lns3.scpt
on error eText number -1700
lns3.scpt
_support's throwInvalidParameter(a reference to item i of theList, "", number, "List item is wrong type: " & eText)
lns3.scpt
_error("max", eText, eNumber, eFrom, eTo)
lns3.scpt
to round number num to places decimalPlaces : (0) by roundingDirection : (rounding halves to even)
lns3.scpt
set num to _support's asRealParameter(num, "")
lns3.scpt
set decimalPlaces to _support's asIntegerParameter(decimalPlaces, "to places")
lns3.scpt
if decimalPlaces ≠ 0 then
lns3.scpt
set theMultiplier to 10 ^ decimalPlaces
lns3.scpt
set num to num * 100 * theMultiplier -- multiplying and dividing by 100 before and after applying the multiplier helps avoid poor rounding results for some numbers due to inevitable loss of precision in floating-point math (e.g. `324.21 * 100 div 1 / 100` returns 324.2 but needs to be 324.21), though this hasn't been tested on all possible values for obvious reasons -- TO DO: shouldn't /10 be done after rounding is applied (in which case following calculations should use mod 10, etc)?
lns3.scpt