| 1 |
#charset "us-ascii" |
| 2 |
|
| 3 |
/* |
| 4 |
* Copyright 2000, 2006 Michael J. Roberts |
| 5 |
* |
| 6 |
* TADS 3 library - number handling. This module provides utility |
| 7 |
* functions for converting numbers to strings in various formats. |
| 8 |
*/ |
| 9 |
|
| 10 |
#include "adv3.h" |
| 11 |
|
| 12 |
|
| 13 |
/* ------------------------------------------------------------------------ */ |
| 14 |
/* |
| 15 |
* Format a number as a binary string. |
| 16 |
*/ |
| 17 |
intToBinary(val) |
| 18 |
{ |
| 19 |
local result; |
| 20 |
local outpos; |
| 21 |
|
| 22 |
/* if the value is zero, the binary representation is simply '0' */ |
| 23 |
if (val == 0) |
| 24 |
return '0'; |
| 25 |
|
| 26 |
/* allocate a vector to store the characters of the result */ |
| 27 |
result = new Vector(33).fillValue(nil, 1, 33); |
| 28 |
|
| 29 |
/* |
| 30 |
* Fill up the vector with 1's and 0's, working from the lowest-order |
| 31 |
* bit to the highest-order bit. On each iteration, we'll pull out |
| 32 |
* the low-order bit, add it to the result string, and then shift the |
| 33 |
* value right one bit so that the next higher-order bit becomes the |
| 34 |
* low-order bit in the value. |
| 35 |
*/ |
| 36 |
for (outpos = 34 ; val != 0 ; val >>= 1) |
| 37 |
{ |
| 38 |
/* |
| 39 |
* Add the next bit. Add it at the end of the string so the |
| 40 |
* final result reads left-to-right, high-to-low. Note that |
| 41 |
* Unicode value 0x30 is the digit '0', and Unicode value 0x31 is |
| 42 |
* the digit '1'. We build the result as a vector of Unicode |
| 43 |
* values for efficiency, so that we don't have to repeatedly |
| 44 |
* allocate partial strings. |
| 45 |
*/ |
| 46 |
result[--outpos] = ((val & 1) == 0 ? 0x30 : 0x31); |
| 47 |
} |
| 48 |
|
| 49 |
/* convert the vector of Unicode characters to a string */ |
| 50 |
return makeString(result.toList(outpos, 34 - outpos)); |
| 51 |
} |
| 52 |
|
| 53 |
/* ------------------------------------------------------------------------ */ |
| 54 |
/* |
| 55 |
* Convert an integer number to Roman numerals. Returns a string with |
| 56 |
* the Roman numeral format. This can only accept numbers in the range |
| 57 |
* 1 to 4999; returns nil for anything outside of this range. |
| 58 |
*/ |
| 59 |
intToRoman(val) |
| 60 |
{ |
| 61 |
local str; |
| 62 |
local info = |
| 63 |
[ |
| 64 |
/* numeral value / corresponding string */ |
| 65 |
1000, 'M', |
| 66 |
900, 'CM', |
| 67 |
500, 'D', |
| 68 |
400, 'CD', |
| 69 |
100, 'C', |
| 70 |
90, 'XC', |
| 71 |
50, 'L', |
| 72 |
40, 'XL', |
| 73 |
10, 'X', |
| 74 |
9, 'IX', |
| 75 |
5, 'V', |
| 76 |
4, 'IV', |
| 77 |
1, 'I' |
| 78 |
]; |
| 79 |
local i; |
| 80 |
|
| 81 |
/* if the value is outside of the legal range, fail immediately */ |
| 82 |
if (val < 1 || val > 4999) |
| 83 |
return nil; |
| 84 |
|
| 85 |
/* |
| 86 |
* iterate over the specifiers and apply each one as many times as |
| 87 |
* possible |
| 88 |
*/ |
| 89 |
for (str = '', i = 1 ; val != 0 ; ) |
| 90 |
{ |
| 91 |
/* |
| 92 |
* If we're greater than the current specifier, apply it; |
| 93 |
* otherwise, move on to the next specifier. |
| 94 |
*/ |
| 95 |
if (val >= info[i]) |
| 96 |
{ |
| 97 |
/* add this specifier's roman numeral into the result */ |
| 98 |
str += info[i+1]; |
| 99 |
|
| 100 |
/* subtract the corresponding value */ |
| 101 |
val -= info[i]; |
| 102 |
} |
| 103 |
else |
| 104 |
{ |
| 105 |
/* move to the next specifier */ |
| 106 |
i += 2; |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
/* return the result */ |
| 111 |
return str; |
| 112 |
} |
| 113 |
|