;==================== begin sigfigs.nls ========================================
; sigfigs - Like precision but specifies number of significant figures.
;
; By Rik Blok, 2013 <http://www.zoology.ubc.ca/~rikblok/wiki/doku.php>
;
; This is free and unencumbered software released into the public domain.
;
; Anyone is free to copy, modify, publish, use, compile, sell, or
; distribute this software, either in source code form or as a compiled
; binary, for any purpose, commercial or non-commercial, and by any
; means.
;
; Provides:
;
;   sigfigs
;     Like precision but specifies number of significant figures. 
;   sigfigs-other
;     Like sigfigs but to same precision as another number.
;
; Usage:
;   
;   ; Reports _value_ to _number_ sigfigs.
;   sigfigs  _value_  _number_
;
;   ; Reports _value_ to same precision as first _number_ sigfigs of _other-value_.
;   sigfigs-other  _value_  _other-value_  _number_
;
; Example:
;
;   ; print pi to 3 sigfigs (3.14)
;   print sigfigs pi 3
;
;   ; print ticks to same digit as dt
;   print sigfigs dt 2
;   tick-advance dt
;   print sigfigs-other ticks dt 2
;
; Revisions:
;
;   2013-08-29 - handle negative numbers and zero
;   2013-08-03 - initial release by Rik Blok
;-------------------------------------------------------------------------------


to-report sigfigs 
; Reports first _digits_ of _number_.
; Parameters:
[ number ; Number to report.
  digits ; Report this many significant figures.
]
  if number = 0 [ report 0 ]
  report precision number (digits - ceiling log abs number 10)
end


to-report sigfigs-other
; Reports _number_ to same decimal as _digits_ sigfigs of _other-number_.
[ number       ; Number to report.
  other-number ; Other number to match sigfigs.
  digits       ; Significant figures of other number to match.
]
  if other-number = 0 [ report sigfigs number digits ]
  report precision number (digits - ceiling log abs other-number 10)
end
;==================== end sigfigs.nls ==========================================