; IEEE 754 Single Precision Simulation ; written by Teresa Carrigan, 2004 globals [ from-number start-x praise digits myDigits doneyet? number-of-digits step case-type ] breeds [ sign exponent mantissa] patches-own [ name ] ; runs setup when program is first loaded to startup setup end ; initializes variables to setup locals [ here-x here-y current n] ca set praise [ "You got it!" "Right!" "Correct" "Awesome!" "Perfect!" ] set digits [ "0" "1" "2" "3" "4" "5" "6" "7" "8" "9" "A" "B" "C" "D" "E" "F" ] set myDigits [] set n 0 repeat 2 [ set myDigits lput (item n digits ) myDigits set n (n + 1) ] set start-x ( .5 * screen-size-x) set here-x (start-x - 3) set here-y 3 set current 1 set n 0 set number-of-digits 32 set case-type "standard" set from-number "" ; make a random 32-bit pattern repeat number-of-digits [ set from-number (word from-number random 2) ] if special-bit-patterns [ generate-special ] ; create explanation bars at top and bottom ask patches with [ pycor < -4 or pycor > 4] [ set pcolor blue ] ask patch-at 6 6 [ set plabel "IEEE 754 Single Precision Format" set plabel-color white set name -1 ] ask patch-at 8 5 [ set plabel "number here" set plabel-color white set name -2 ] ask patch-at 6 -5 [ set plabel "Start with 32 bits." set plabel-color white set name 1 ] ask patch-at 6 -6 [ set plabel "" set name 2 ] explain -2 "Bit pattern: " + add-spaces from-number set step 1 end to explain [ which what ] ask patches with [ name = which ] [ set plabel what ] end ; do a single step (whichever comes next) to one-step locals [ which-step] set which-step "step" + step ifelse ( step < 8) [ run which-step ] [ if step = 9 [finish-special] ] end ; determine sign to step1 locals [ tag setcolor] set setcolor red set tag first from-number explain 1 "The first bit tells us the sign." cct-sign 1 [ set shape "circle" set color pcolor set label-color setcolor set label tag + " " set size 0 setxy -4 5 ] ifelse tag = "0" [ explain 2 "0: Number is positive." ] [ explain 2 "1: Number is negative." ] ask sign [ wander-to -12 2 ask patch-at 0 -2 [ set plabel "Sign" set plabel-color setcolor ] ask patch-at 0 -3 [ set plabel-color setcolor set name "sign" ifelse tag = "0" [set plabel "+ "] [set plabel "- "] ] ] set step (step + 1) end ; reports true if every character of string x is = d to-report all-same [ x d ] locals [ result ch k ] set result true set k 0 while [ k < length x ] [ set ch item k x if ch != d [ set result false ] set k (k + 1) ] report result end ; does the bit pattern represent zero, infinity, or NaN? to check-for-special-case [ tag ] locals [ mant mantzero?] set mant substring from-number 9 32 set mantzero? (all-same mant "0") set tag remove " " tag ifelse (all-same tag "0" ) and mantzero? [ set case-type "zero" ] [ if (all-same tag "1") [ ifelse mantzero? [ set case-type "infinity"] [ set case-type "Nan" ] ] ] end ; return randomly "0" or "1" to-report random-bit locals [ ch ] ifelse random 100 < 50 [ set ch "0" ] [ set ch "1" ] report ch end ; generate a special bit pattern, but sometimes it will be a normal number to generate-special locals [ ch ] ; set sign bit (either 0 or 1) set from-number random-bit ; set exponent (either all 0s or all 1s) set ch random-bit repeat 8 [ set from-number (word from-number ch) ] ; set mantissa ifelse ch = "1" ; either infinity or NaN [ ifelse random 100 < 50 [ set ch "0" ; infinity repeat 23 [ set from-number (word from-number ch) ] ] [ repeat 23 ; NaN [ set ch random-bit set from-number (word from-number ch) ] ] ] [ ; either zero or regular number ifelse random 100 < 50 [ set ch "0" ; zero repeat 23 [ set from-number (word from-number ch) ] ] [ repeat 23 ; regular number [ set ch random-bit set from-number (word from-number ch) ] ] ] end ; finish up when a zero bit pattern is detected to handle-zero-case set step 8 ask exponent [ ask patch-at 0 -3 [set plabel " 0 " set plabel-color cyan set name "Exponent" ] ] set-mant explain 1 "Exponent bits are all 0, mantissa bits are all 0." explain 2 "" wait slow-motion end ; last step when a special bit pattern is detected to finish-special locals [ plus expon num ] ask patches with [ name = "sign" ] [ set plus plabel ] ask sign [ set plabel label set plabel-color label-color set label plus wander-to -1 -6 set color blue set size 1 ] ask patches with [ name = "Exponent" ] [ set expon plabel ] ask exponent [ set plabel label set plabel-color label-color set label expon wander-to 1 -6 set color blue ] end ; finish up when an infinity bit pattern is detected to handle-infinity-case set step 8 ask exponent [ ask patch-at 0 -3 [set plabel "infinity" set plabel-color cyan set name "Exponent" ] ] set-mant explain 1 "Exponent bits are all 1, mantissa bits are all 0." explain 2 "" wait slow-motion end ; finish up when "not a number" bit pattern is detected to handle-Nan-case set step 8 ask exponent [ ask patch-at 0 -3 [set plabel "NaN" set plabel-color cyan set name "Exponent" ] ] set-mant explain 1 "Exponent bits are all 1, but mantissa is not all 0." explain 2 "" wait slow-motion end ; determine the exponent to step2 locals [ tag set-color] set set-color cyan set tag reverse (substring from-number 0 9) set tag add-spaces tag set tag reverse tag set tag but-first tag explain 1 "Next 8 bits tell us the exponent of 2." explain 2 "This is in excess 127 binary notation." cct-exponent 1 [ set shape "circle" set color pcolor set size 0 set label tag set label-color set-color setxy -4 5 ] ask exponent [ wander-to -4 2 ask patch-at 0 -2 [ set plabel "Exponent" set plabel-color set-color ] ] ; check for zero, infinity, NaN check-for-special-case tag set step (step + 1) end ; convert exponent to excess 127 decimal format to step3 locals [ tag num set-color ] ifelse case-type = "standard" [ set set-color cyan set tag (substring from-number 1 9) set num calc-dec tag explain 1 "Converting " + tag + " to decimal gives " + num + "." explain 2 "Now in excess 127 decimal format." ask patch-at -4 -1 [ set plabel num set plabel-color set-color ] ] [ handle-special-case ] wait slow-motion set step (step + 1) end to handle-special-case locals [ which ] set which (word "handle-" case-type "-case") run which end ; convert exponent to normal decimal format to step4 locals [ num expon set-color] set set-color cyan ask patch-at -4 -1 [ set num plabel set plabel-color set-color ] set expon num - 127 explain 1 "Subtracting 127 from " + num + " gives " + expon + "." explain 2 "This is the correct exponent of two." ask patch-at -4 -2 [ set plabel expon set name "Exponent" set plabel-color set-color ] wait slow-motion set step (step + 1) end ; determine the mantissa - only used when special bit pattern is detected to set-mant locals [tag] set tag add-spaces (substring from-number 9 32) cct-mantissa 1 [ set shape "circle" set color pcolor set label-color yellow set size 0 set label tag setxy 3 5 ] ask mantissa [ wander-to 10 2 ask patch-at 0 -2 [ set plabel-color yellow set plabel "Mantissa" ] ] end ; determine the mantissa - used for regular bit patterns to step5 locals [tag] set tag add-spaces (substring from-number 9 32) explain 1 "The remaining bits are the mantissa." explain 2 "Mantissa: " + tag cct-mantissa 1 [ set shape "circle" set color pcolor set size 0 set label-color yellow set label tag setxy 3 5 ] ask mantissa [ wander-to 10 2 ask patch-at 0 -2 [ set plabel-color yellow set plabel "Mantissa" ] ] set step (step + 1) end ; determine the significand. That is, the mantissa with "1." in front of it. to step6 locals [ mant num] ask mantissa [ set mant label ] set num "1." + mant explain 1 "Place 1. in front of the mantissa." explain 2 num ask patch-at 10 -2 [ set plabel-color yellow set plabel num set name "Number" ] wait slow-motion set step (step + 1) end ; last step for regular bit patterns - display answer to step7 locals [ plus expon num ] explain 1 "Combining the pieces:" explain 2 "" ask patches with [ name = "sign" ] [ set plus plabel ] ask sign [ set plabel label set plabel-color label-color set label plus wander-to -6.5 -6 set color blue set size 1 ] ask patches with [ name = "Exponent" ] [ set expon plabel ] ask exponent [ set plabel label set plabel-color label-color set label " x 2^ " + expon wander-to 7 -6 set color blue ] ask patches with [ name = "Number" ] [ set num plabel ] ask mantissa [ set plabel label set plabel-color yellow set label num wander-to 4 -6 set color blue ] wait slow-motion explain 1 "Now in Binary Scientific Notation." set step 99 end ; converts a binary integer (in a string) to a decimal integer to-report calc-dec [ x ] locals [ result n ] set result 0 while [length x > 0] [ set result 2 * result set n read-from-string first x set result result + n set x but-first x ] report result end ; move a turtle to a given set of coordinates, slowly to wander-to [ to-x to-y ] locals [ n ] while [ (ycor != to-y) or (xcor != to-x) ] [ set heading towardsxy-nowrap to-x to-y set n (distancexy to-x to-y) ifelse n > 1 [ fd 1 ] [ setxy to-x to-y ] wait slow-motion ] end ; show the remaining steps of the example to go locals [ which-step] set which-step "step" + step ifelse step < 8 [ run which-step set which-step "step" + step wait slow-motion * 3 ] [ if step = 9 [ finish-special ] stop ] end ; start the same example again to show-again locals [ here-x here-y current n] set start-x ( .5 * screen-size-x) set here-x (start-x - 3) set here-y 3 set current 1 set n 0 ask turtles [ die ] ask patches [ set plabel "" ] explain -1 "IEEE 754 Single Precision Format" explain 1 "Start with 32 bits." explain 2 "" explain -2 "Bit pattern: " + add-spaces from-number set step 1 end ; add a space every four bits, so the user won't make copy errors to-report add-spaces [ number ] locals [ save k ] set save "" set k 0 while [ (length number) > 0 ] [ set save (word last number save ) set number butlast number set k (k + 1) if (k = 4) and (length number > 0) [ set save (word " " save ) set k 0 ] ] set number save report number end ; *** NetLogo Model Copyright Notice *** ; ; Copyright 2004 by Teresa W. Carrigan. All rights reserved. ; ; Permission to use, modify or redistribute this model is hereby granted, ; provided that both of the following requirements are followed: ; a) this copyright notice is included. ; b) this model will not be redistributed for profit without permission ; from Teresa W. Carrigan. ; Contact Teresa W. Carrigan for appropriate licenses for redistribution ; for profit. ; ; To refer to this model in academic publications, please use: ; Carrigan, T. (2004). IEEE 754 Single Precision Simulation model. ; Blackburn College, Carlinville IL. ; ; In other publications, please use: ; Copyright 2004 by Teresa W. Carrigan. All rights reserved. ; ; *** End of NetLogo Model Copyright Notice *** @#$#@#$#@ GRAPHICS-WINDOW 3 10 688 366 13 6 25.0 1 18 1 1 1 CC-WINDOW 13 404 394 467 Command Center BUTTON 9 369 83 402 NIL setup NIL 1 T OBSERVER T SLIDER 327 371 500 404 slow-motion slow-motion 0 1 0.2 0.1 1 seconds BUTTON 84 369 158 402 step one-step NIL 1 T OBSERVER T BUTTON 159 369 234 402 go go T 1 T OBSERVER T SWITCH 502 371 665 404 special-bit-patterns special-bit-patterns 1 1 -1000 BUTTON 236 369 310 402 NIL show-again NIL 1 T OBSERVER T @#$#@#$#@ WHAT IS IT? ----------- This model demonstrates how the 32-bit IEEE 754 Single Precision representation of a floating point number is interpreted. HOW IT WORKS ------------ A random 32-digit bit pattern is generated. The first bit tells us the sign. The next eight bits give us the exponent of two. The last 23 bits are the fraction part of the number. HOW TO USE IT ------------- The setup button generates a random bit pattern. The special-bit-patterns switch determines the probability that one of the special bit patterns will be generated when you click the setup button. If the switch is on, then about 75% of the time the pattern generated will be interpreted as either zero, infinity, or NaN ("not a number", used when a program tries to divide by zero or take the square root of a negative number, etc.). If the special-bit-patterns switch is off, then almost all of the time the pattern generated will represent a normal number, but there is still a small chance that one of the special patterns will be generated. The slow-motion slider is an easy way to adjust the speed of the display. Set it to zero if you want to show the final result as quickly as possible. 0.2 is a good setting for most purposes. The step button does whichever step of the conversion process comes next. It then stops so you can take notes. This is useful when you are first learning the method. The go button does the entire conversion, at a speed determined by the slow-motion slider. This is useful when you do not need to take notes between each step, or do not wish to press the step button seven times to get an answer. The show-again button starts the exact problem from the beginning. You may then click either the step button or the go button to see the same demonstration. THINGS TO NOTICE ---------------- Besides the special bit patterns that represent positive and negative infinity, what is the largest number that this format can store? The most negative? The smallest positive number? When the bit pattern is one of the special ones, we do not have to convert the exponent to decimal. THINGS TO TRY ------------- Set slow-motion to 0.2, click setup, and then click go. Set the special-bit-patterns switch to the type of problem you want to drill, then click setup. Attempt one step at a time on paper, and then click the step button to check that you did that step correctly. EXTENDING THE MODEL ------------------- Allow the user to input a starting bit pattern. Allow the user to input a number in Binary Scientific Notation, and then display the corresponding bit pattern. Allow the user to input a decimal number, and then display the corresponding bit pattern. Display the decimal equivalent of the bit pattern. NETLOGO FEATURES ---------------- one-step uses the NetLogo run command combined with a global integer variable step to run the next step, without needing nested ifelse blocks. handle-special-case uses a similar trick to call the appropriate special case handling procedure, without using ifelse blocks. CREDITS AND REFERENCES ---------------------- This model was written by Teresa W. Carrigan, 2004. Permission to use, modify or redistribute this model is hereby granted, provided that both of the following requirements are followed: a) this copyright notice is included. b) this model will not be redistributed for profit without permission from Teresa W. Carrigan. Contact Teresa W. Carrigan for appropriate licenses for redistribution for profit. To refer to this model in academic publications, please use: Carrigan, T. (2004). IEEE 754 Single Precision Simulation model. Blackburn College, Carlinville, IL. In other publications, please use: Copyright 2004 by Teresa W. Carrigan. All rights reserved. FOR MORE INFORMATION -------------------- For more information on IEEE 754 Single Precision floating point numbers, see one of the following textbooks: [1] Murdocca, M. and Heuring, V. "Principles of Computer Architecture", First Edition, Prentice Hall, pages 45-47 [2] Bronson, G. "Java for Engineers and Scientists", First Edition, Thomson/BrooksCole, pages 799-801. [3] Tanenbaum, A. "Structured Computer Organization", Fourth Edition, Prentice Hall, pages 646-650. @#$#@#$#@ default true 0 Polygon -7566196 true true 150 5 40 250 150 205 260 250 box true 0 Polygon -7566196 true true 45 255 255 255 255 45 45 45 circle false 0 Circle -7566196 true true 35 35 230 @#$#@#$#@ NetLogo 2.0.1 @#$#@#$#@ @#$#@#$#@ @#$#@#$#@