<< Home | About Forth | About TurboForth | Download | Language Reference | Resources | Tutorials | YouTube >>


SIGN Search:

In Double-Precision Numeric Display Words in 32-bit library

Word Name: SIGN
Type: Standard word
Data Stack Signature: n --
Return Stack Signature: --
Availability: V1.2
Description:

Appends the sign of the number currently being built in the PNO buffer.

Example:

: TEST TUCK DABS <# #S ROT SIGN #> TYPE SPACE ;

-12345678. TEST (displays -12345678)
12345678. TEST (displays 12345678)

The above code first TUCKs the high order cell of the double value (which contains the sign of the number in the most significant bit) and stores it on the data stack, underneath the double value. The double value is then forced positive using DABS. Next, all digits of the number are added to the PNO buffer via #S, leaving the address and length of the resultant string on the stack (with the saved high-order value underneath it). The previously saved high-order cell is then ROTated into position, and fed to SIGN. If the high-order cell is negative, SIGN will insert a minus symbol (-) into the PNO buffer, otherwise it will take no action.

Comment:

If the number is negative (i.e. if its most significant bit is on) then a - (negative) is added to the buffer. If the number is positive (MSB=0), SIGN performs no action.

Note that SIGN takes a single signed value as its input. When using <# #> to display a signed-double value, one will normally copy the high-order word of the double value, and store it somewhere (e.g. in a variable or on the return stack) to feed to SIGN later.

Caution: When storing data on the return stack, it is crucial that the return stack be restored before the colon definition exits. Additionally, the return stack should not be used to hold data inside of a loop. An alternative to the return stack would be to use the word TUCK to store a copy of the high-order cell underneath the double number, as in:

: TEST TUCK DABS <# #S ROT SIGN #> TYPE SPACE ;

See Also: <#  #  #>  #S 

<< Home | About Forth | About TurboForth | Download | Language Reference | Resources | Tutorials | YouTube >>