~ubuntu-fr-scripts/ufrs-math/math-dev

« back to all changes in this revision

Viewing changes to bc-lib/pdhi.bc

  • Committer: Jean-Michel Juzan
  • Date: 2009-04-01 12:03:01 UTC
  • Revision ID: jean-michel@juzan.org-20090401120301-j6g6xj1z0561zdg3
copie de la branche principale

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# pdhi - Pan Digital Halving Index with GNU bc
 
2
# Returns how many times x must be divided by 2 before
 
3
# the result contains all digits from 0 to 9 (if ibase = 10).
 
4
# e.g. 3339 -> 1669.5 -> 834.75 -> 417.375 ->
 
5
#      208.6875 -> 104.34375 -> 52.171875 ->
 
6
#      26.0859375 -> 13.04296875, i.e. 8 times
 
7
 
 
8
# Uses ibase as the base for divisions (usually 10)
 
9
 
 
10
define pdhi(x) {
 
11
  auto d[],xi,xf,c,r,pdhi,lim,i;
 
12
  if(x<0)x*=-1
 
13
  c=1;pdhi=-1;lim=int(10/ibase+3)*scale
 
14
  while(c){
 
15
    pdhi+=1
 
16
    xi=int(x);xf=x-xi
 
17
    while(xi){
 
18
      r=int(xi/ibase)
 
19
      d[xi-ibase*r]=1
 
20
      xi=r
 
21
    }
 
22
    for(i=lim ; i && xf ; i--){
 
23
    #while(xf){
 
24
      xf*=ibase
 
25
      r=int(xf)
 
26
      d[r]=1
 
27
      xf-=r
 
28
    }
 
29
    c=ibase
 
30
    for(r=0;r<ibase;r++){c-=d[r];d[r]=0}
 
31
    x/=2
 
32
  }
 
33
  return pdhi;
 
34
}
 
35
 
 
36
# pdmi(x, m) - Pan Digital Multiplying Index
 
37
# Returns how many times x must be multiplied by m before
 
38
# the result contains all digits from 0 to 9 (if ibase = 10).
 
39
# e.g. pdmi(3339,0.5) -> 1669.5 -> 834.75 -> 417.375 ->
 
40
#      208.6875 -> 104.34375 -> 52.171875 ->
 
41
#      26.0859375 -> 13.04296875, i.e. 8 times
 
42
 
 
43
# Uses ibase as the base for divisions (usually 10)
 
44
 
 
45
define pdmi(x,m) {
 
46
  auto d[],xi,xf,c,r,pdmi,lim,i;
 
47
  if(x<0)x*=-1
 
48
  c=1;pdmi=-1;lim=int(10/ibase+3)*scale
 
49
  while(c){
 
50
    pdmi+=1
 
51
    xi=int(x);xf=x-xi
 
52
    while(xi){
 
53
      r=int(xi/ibase)
 
54
      d[xi-ibase*r]=1
 
55
      xi=r
 
56
    }
 
57
    for(i=lim ; i && xf ; i--){
 
58
    #while(xf){
 
59
      xf*=ibase
 
60
      r=int(xf)
 
61
      d[r]=1
 
62
      xf-=r
 
63
    }
 
64
    c=ibase
 
65
    for(r=0;r<ibase;r++){c-=d[r];d[r]=0}
 
66
    x*=m
 
67
  }
 
68
  return pdmi;
 
69
}