Iterate/Floating-point
Jump to navigation
Jump to search
This implements floating point algorithms into Iterate, following IEEE standard. For performance, the exponent and mantissa will be separate.
Programs are uncompiled.
Int to float
(*)1<
*?< (int*)<> (cur*)<> > // get integer number
*1< (shr_len*)<> >
// shift right until we reach the most significant bit
(shr*)∞<
(is1*)=cur< // true if we only have one bit (the MSB)
*~n< !is1 >
!shr
>
$shr_remainder $shr_quotient
$shr_initial $shr_countdown
*2< (shr_initial*)<> (shr_countdown*)<> >
(shr_div*)=cur< // shift right (divide by two)
(shr_remainder*)<>
$shr_decrement
*=shr_countdown< (shr_decrement*)<> >
$shr_countdown
*=shr_decrement<
*~n< (shr_countdown*)<> >
!
>
*=shr_countdown< &shr_div >
$shr_remainder // the popped bit
*=shr_initial< (shr_countdown*)<> >
(shr_quotient*)<> // remaining bits
>
*=shr_remainder< *=shr_len< (shr_stack*)<> > > // add to the stack
$shr_len_temp
*2< *=shr_len< (shr_len_temp*)<> > > // multiply length by two for next value
$shr_len
*=shr_len_temp< (shr_len*)<> >
(shr_count*)<> // count the amount of shifts
$cur
*=shr_quotient< (cur*)<> > // for the next step
>
*23< (pad_cur*)<> > // 23 zeroes
*=shr_count< // decrement 23 by the existing bits (i.e 3 bits -> 20 pad)
$pad_decrement
*=pad_cur<
*~n< (pad_decrement*)<> >
!
>
$pad_cur
*=pad_decrement< (pad_cur*)<> >
>
*=pad_cur< // shift left the stack by the padding by multiplying by two a bunch
$shr_stack_temp
*2< *=shr_stack< (shr_stack_temp*)<> > >
$shr_stack
*=shr_stack_temp< (shr_stack*)<> >
>
*127< (shr_count*)<> > // exponent + 127 for the bias
(out_bias*)=shr_count< // print the bias
*~n< &out_bias >
@
>
*1< (newline*)10< *~n< &newline > ~@ ! > >
(out_stack*)=shr_stack< // print the mantissa
*~n< &out_stack >
@
>
>