comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » Re: How to plot multiple charts and ho to round float to specific precision?
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Return to the default flat view Create a new topic Submit Reply
Re: How to plot multiple charts and ho to round float to specific precision? [message #44963 is a reply to message #44857] Thu, 28 July 2005 12:29 Go to previous messageGo to previous message
Patrick Broos is currently offline  Patrick Broos
Messages: 27
Registered: December 1996
Junior Member
I was faced with the general case of this problem, i.e. where you don't
know the magnitude of the number but you want to limit it to a certain
number of significant digits, and was moved to write the routine below.
There may be a much easier way. :)

Cheers,
Patrick Broos

;+
;=========================================================== =============
;;;
;;; FILE NAME: @(#)limit_precision.pro 8.5
;;;
;;; DESCRIPTION: This routine converts a real number to a string with
the
;;; specified number of significant digits.
;;;
;;; AUTHOR: Pat Broos (patb@astro.psu.edu)
;;; Scott Koch (tsk@astro.psu.edu)
;;; Copyright (C) 1996, Pennsylvania State University
;;;-
;=========================================================== =============
FUNCTION limit_precision, value, sig_digits

if (value EQ 0) then begin
return, '0.0'
endif


;; Calculate an increment to add or subtract to value.
log_value = alog10( abs(value) )

increment = 10.0D^(ceil( log_value ) - sig_digits)


;; Determine whether we want to add to value (round UP) or
;; subtract from value (round DOWN) in order to achieve the
;; result that ROUND() would do if the significant digits
;; were shifted to the integer part of the number.
shifted_value = value/increment
round_up = (shifted_value LT round(shifted_value))

;; Adjust the value by increment until the desired condition is met.
fmt = string( sig_digits, f='("(G20.",I0,")")' )

done = 0
steps = 0
while (NOT done) do begin
if (round_up) then begin
rounded_string = string( value + steps*increment, f=fmt )
done = (value LE float( rounded_string ))
endif else begin
rounded_string = string( value - steps*increment, f=fmt )
done = (value GE float( rounded_string ))
endelse

steps = steps + 1
if (steps GE 100) then message, 'Loop failed to converge.'
endwhile

return, strcompress( rounded_string, /REMOVE_ALL )
end
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Text widget error checking
Next Topic: Re: Text widget error checking

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Fri Oct 10 16:26:24 PDT 2025

Total time taken to generate the page: 1.04612 seconds