--
Deep Fryer: A very philosophical monk.
Website @
http://webpages.charter.net/dawill/tmoranwms
krw said:
Grrr! I still can't find a binary calculator that knows what a
binary point is or where to put one.
On a similar subject, I wanted to know what numbers look like in weird
bases like base 5 or base pi, so I wrote this:
-=-=-
DECLARE FUNCTION Character$ (Value%)
CONST Pi# = 3.141592653589793#
CONST e# = 2.718281828459045#
' Obtain inputs
INPUT "Enter number to convert: ", Number#
BadBase:
INPUT "Enter base to convert from decimal to (enter Pi or e for the
constants, or sqr to enter a root): ", BaseString$
BaseString$ = UCASE$(BaseString$)
IF BaseString$ = "PI" THEN
BaseNumber# = Pi#
ELSEIF BaseString$ = "E" THEN
BaseNumber# = e#
ELSEIF BaseString$ = "SQR" THEN
INPUT "Enter number to take the square root of: ", Root#
BaseNumber# = SQR(ABS(Root#)) 'Neener, no imaginary base for you.
ELSE
BaseNumber# = VAL(BaseString$)
END IF
IF BaseNumber# <= 1 THEN PRINT "Base must be greater than 1.": GOTO BadBase
IF BaseNumber# >= 62 THEN PRINT "Base must be lesser than or equal to 62.":
GOTO BadBase
' Find the largest power in the input
LargestPower% = FIX(LOG(Number#) / LOG(BaseNumber#))
IF Number# < 1 THEN LargestPower% = LargestPower% - 1
' Double precision is only 16 digits, so it makes sense to carry this
' to only as many decimals, plus room for an exponent.
DIM Result AS STRING * 30
FOR i% = 0 TO 15
' Pare down the input iteratively, counting the multiple each time
' and appending (or insert) it to the output string.
TestPower# = BaseNumber# ^ (LargestPower% - i%)
j% = 0
WHILE Number# - TestPower# >= 0
Number# = Number# - TestPower#
j% = j% + 1
WEND
IF i% = 0 THEN
' Set the decimal point in scientific notation.
MID$(Result, i% + 1, 2) = Character$(j%) + "."
ELSE
MID$(Result, i% + 2, 1) = Character$(j%)
END IF
NEXT
IF Decimal = 0 THEN MID$(Result, i% + 1) = "E" +
LTRIM$(RTRIM$(STR$(LargestPower%)))
PRINT Result
' Returns an ASCII character corresponding to Value.
' 0 through 9: as usual.
' 10 through 35: capital letters (the first five, having values 10, 11,
' 12, 13, 14 and 15 come through as A, B, C, D, E and F for hexadecimal;
' the rest of the alphabet follows similarly).
' 36 through 61: running out of choices, so going with lower case
letters.
' I don't know of any standard numerals for values this large.
FUNCTION Character$ (Value%)
SELECT CASE Value%
CASE 0 TO 9
Character$ = LTRIM$(RTRIM$(STR$(Value%)))
CASE 10 TO 35
' Capital letters, starting at index 65 (10 + 55)
Character$ = CHR$(Value% + 55)
CASE 36 TO 61
' Lowercase letters, starting at index 97 (36 + 61)
Character$ = CHR$(Value% + 61)
CASE ELSE
' Should never come up, but just in case.
Character$ = CHR$(32)
END SELECT
END FUNCTION
-=-=-
Probably quite slow (precision exponents? eww...), but it's not much and
probably completes in a fraction of a milisecond, so who cares.
Come to think of it, the real datatypes are binary, so you could also print
the first 48 (IIRC) bits of the DOUBLE to get its representation in binary,
plus whatever exponent in the following word.
Tim