Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado
You try the following. ;------start ;+ ; NAME: ; ROUNDPV ; ; PURPOSE: ; This function returns a rounded value to a specified place value for Integer, Long, Float, And ; Double precision values. ; ; CATEGORY: ; Miscellaneous, Math ; ; CALLING SEQUENCE: ; Result = RoundPV(Value, PlaceValue) ; ; ; INPUTS: ; Value A scalar or vector of type integer, long integer, float or double precisions. ; PlaceValue An integer specifying the plave value to round toward. Positive and negative values ; are permitted. ; ; OPTIONAL INPUTS: ; None ; ; KEYWORD PARAMETERS: ; None ; ; OUTPUTS: ; This function returns the rounded value to the spcified place value. ; ; OPTIONAL OUTPUTS: ; None ; ; COMMON BLOCKS: ; None. ; ; SIDE EFFECTS: ; None known. ; ; RESTRICTIONS: ; None known. ; ; EXAMPLE: ; X = 321.489 ; For i = -2, 2 Do Print, RoundPV(X, i) ; 321.480 ; 321.400 ; 321.000 ; 320.000 ; 300.000 ; ; Specifying a negative place value for an integer or long type data value has no effect. For example ; X = 321L ; For i = -2, 2 Do Print, RoundPV(X, i) ; 321 ; 321 ; 321 ; 320 ; 300 ; ; MODIFICATION HISTORY: ; Written by: Ben Tupper 30 SEP 1999 ; email pemaquidriver@tidewater.net ; tel: (207) 563 - 1048 ; 248 Lower Round Pond Road ; POB 106 ; Bristol, ME 04539-0106 ; ; 6 FEB 2000 Dropped call to home grown TYPE function in favor of SIZE function ; with STRUCTURE keyword set. ; ;- FUNCTION RoundPV, X, PV If N_elements(PV) EQ 0 Then PV = 0 Sz = Size(X, /Str) Case Sz.Type of 2: X2 = Fix(Float(Long(Float(X)*10.^(-PV)))*10.^PV) 3: X2 = Long(Float(Long(FLoat(X)*10.^(-PV)))*10.^PV) 4: X2 = Float(Long(X*10.^(-PV)))*10.^PV 5: X2 = Double(Long(X*10.d^(-PV)))*10.d^PV Else: EndCase Return, X2 END ;------finish Med Bennett wrote: > Has anyone written a function that returns the input value or array with > a specified number of significant digits? I have lloked at the various > IDL libraries on the web but did not come up with what I'm after. > Thanks in advance for any pointers - -- Ben Tupper 248 Lower Round Pond Road POB 106 Bristol, ME 04539 Tel: (207) 563-1048 Email: PemaquidRiver@tidewater.net
"Med Bennett" <mbennett@indra.com> wrote in message news:3A9ACD30.E0F3F5D9@indra.com... > Has anyone written a function that returns the input value or array with > a specified number of significant digits? I have lloked at the various > IDL libraries on the web but did not come up with what I'm after. > Thanks in advance for any pointers - > this should do it function fix_digits, num, digits expon = 1.0 while num/expon gt 1 do expon = expon * 10 fix_val = num/expon fstring = string(digits+2, digits, format = '("(f",i0.0,".",i0.0,")")') reads, string(fix_val, format = fstring), fix_val return, fix_val * expon end > print, fix_digits(!pi, 3) 3.14000 Martin ---------------------------------------- Mr. Martin Downing, Clinical Research Physicist, Orthopaedic RSA Research Centre, Woodend Hospital, Aberdeen, AB15 6LS. m.downing@abdn.ac.uk Martin
"Ben Tupper" <pemaquidriver@tidewater.net> wrote in message news:3A9AEA4B.E6169330@tidewater.net... > You try the following. > > ;------start > ;+ > ; NAME: > ; ROUNDPV > ; hm, I got funny results with this (as i did with my other attempt!) IDL> print, roundPV( [ 0.0002323,34.3434,1234000], 2) 0.000000 0.000000 1.23400e+006 so I had another think: function fix_digits, num, digits p10 = floor(alog10(abs(num))) expo = 10.0d^(digits -1 - p10) fix_val = long(num*expo)/expo return, fix_val end this seems to be fine for all thise who dont care about casting to double - some one else can reset the type! IDL> print, fix_digits( [ 0.0002323,34.3434,1234000], 2) 0.00023000000 34.000000 1200000.0 Martin
Thanks Ben and Martin! I had already kluged together another solution using a routine I found using the search engine I found using http://www.astro.washington.edu/deutsch/idl/htmlhelp/index.h tml (very handy tool!) by searching for 'significant' called strnsignif.pro ( http://www.astro.washington.edu/deutsch-bin/getpro/library03 .html?STRNSIGNIF). Wrapping this with a loop and converting back to float did the trick. Less than perfect, but in the consulting biz, whatever can be found quickly beats an elegant solution! Med Martin Downing wrote: > "Med Bennett" <mbennett@indra.com> wrote in message > news:3A9ACD30.E0F3F5D9@indra.com... >> Has anyone written a function that returns the input value or array with >> a specified number of significant digits? I have lloked at the various >> IDL libraries on the web but did not come up with what I'm after. >> Thanks in advance for any pointers - >> > > this should do it > > function fix_digits, num, digits > expon = 1.0 > while num/expon gt 1 do expon = expon * 10 > fix_val = num/expon > fstring = string(digits+2, digits, format = '("(f",i0.0,".",i0.0,")")') > reads, string(fix_val, format = fstring), fix_val > return, fix_val * expon > end > >> print, fix_digits(!pi, 3) > 3.14000 > > Martin > > ---------------------------------------- > Mr. Martin Downing, > Clinical Research Physicist, > Orthopaedic RSA Research Centre, > Woodend Hospital, > Aberdeen, AB15 6LS. > m.downing@abdn.ac.uk > > Martin