NADCON5-ng  0.0.1
NADCON5 Next Generation
onzd.f
Go to the documentation of this file.
1 c> \ingroup core
2 c> Function to round a digit to one significant figure (one non zero digit), single precision
3 c>
4 c> Function "onzd" stands for "One Non Zero Digit"
5 c>
6 c> It takes a Real*4 number as input, and rounds that
7 c> number to the closest number containing only 1 non-zero digit.
8 c> The list of such numbers is infifinite, but contain
9 c> these, in order:
10 c>
11 c> 0.7 , 0.8 , 0.9 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9, 10 , 20 , 30 , etc etc
12 c>
13 c> \param[in] x input value
14 c> \return `real*4` rounded value of x to one non zero digit
15 c>
16 c> Examples of input/output are:
17 c>
18 c> 0.000019 => 0.000020
19 c> 0.007432 => 0.007000
20 c> 1.7 => 2.000000
21 c> 9.143 => 9.000000
22 c> 17.4 => 20.000000
23 c> 947.3 => 900.000000
24 c> 987.432 => 1000.000000
25 c> 1014.8 => 1000.000000
26 c> 1502.7 => 2000.000000
27 c>
28  function onzd(x)
29  implicit none
30  real*4 onzd,x,y
31  real*8 q,qten
32  integer*4 imag,iq,isign
33 
34 c - Function "onzd" stands for "One Non Zero Digit"
35 
36 c - It takes a Real*4 number as input, and rounds that
37 c - number to the closest number containing only 1 non-zero digit.
38 c - The list of such numbers is infifinite, but contain
39 c - these, in order:
40 c - 0.7 , 0.8 , 0.9 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9, 10 , 20 , 30 , etc etc
41 c -
42 c - Examples of input/output are:
43 c - 0.000019 => 0.000020
44 c - 0.007432 => 0.007000
45 c - 1.7 => 2.000000
46 c - 9.143 => 9.000000
47 c - 17.4 => 20.000000
48 c - 947.3 => 900.000000
49 c - 987.432 => 1000.000000
50 c - 1014.8 => 1000.000000
51 c - 1502.7 => 2000.000000
52 
53  isign = +1
54  y = x
55  if(x.lt.0)then
56  isign = -1
57  y = -x
58  endif
59 
60 c - 1) Determine magnitude of x, in terms of integer exponent of ten
61  imag = floor(log10(y))
62 
63 c - 1a) Get the multiplier
64  qten = (10.d0**imag)
65 
66 c - 2) Get into a range between 0.0 and 10.0
67  q = dble(y) / qten
68 
69 c - 3) Round to the closest integer (0 through 10)
70  iq = nint(q)
71 
72 c - 4) Scale back to the original size
73  onzd = isign * dble(iq) * qten
74 
75 c write(6,100)x,imag,qten,q,iq,onzd
76 c 100 format(f30.15,1x,i10,1x,f30.15,1x,f30.15,1x,i10,1x,f30.15)
77 
78  return
79  end
80 
real *4 function onzd(x)
Function to round a digit to one significant figure (one non zero digit), single precision.
Definition: onzd.f:29