You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Care of @ajewellamz, some suggestions for useful utilities when working with the JSON library:
functionDecimalToNat(num: Decimal) : Result<nat, string>
{
:-Need(num.n >= 0, "Number must be > 0");
if num.n == 0 thenSuccess(0)
else
:-Need(num.e10 >= 0, "Number must be a whole number");
:-Need(num.e10 < 100, "Number must be less than a googol");
Success(GetPower(num.n, num.e10))
}
functionDecimalToInt(num: Decimal) : Result<int, string>
{
if num.n == 0 thenSuccess(0)
else
:-Need(num.e10 >= 0, "Number must be a whole number");
:-Need(num.e10 < 100, "Number must be less than a googol");
Success(GetPower(num.n, num.e10))
}
functionDecimalToStr(num: Decimal) : Result<string, string>
{
if num.n == 0 thenSuccess("0")
else
:-Need(-1000 < num.e10 < 1000, "Exponent must be between -1000 and 1000");
var str := String.Base10Int2String(num.n);
if num.e10 >= 0 thenSuccess(str + Zeros(num.e10))
elseif-num.e10 < |str| thenvar pos := |str| + num.e10;
Success(str[..pos] + "." + str[pos..])
elseSuccess("0." + Zeros(|str| - num.e10) + str)
}
// Return a string of this many zerosfunctionZeros(n : nat) : (ret : string)
{
seq(n, i => '0')
}
// return n x 10^powfunctionGetPower(n : nat, pow : nat) : nat
{
if pow == 0 then
n
else
10 *GetPower(n, pow-1)
}
The text was updated successfully, but these errors were encountered:
Care of @ajewellamz, some suggestions for useful utilities when working with the JSON library:
The text was updated successfully, but these errors were encountered: