| Path: | lib/rapfp/Ap.rb |
| Last Update: | Sun Apr 29 23:40:49 -0500 2007 |
###
File: Ap.rb
Subject: Main Methods for arbitrary precision.
Author: Dennis J. Darland
Date: April 2, 2007
Copyright (C) 2007 Dennis J. Darland#
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
# File lib/rapfp/Ap.rb, line 32
32: def ap_in(str)
33: found = 0
34: f_mant_re1 = Regexp.new("^-?[0-9]+\.[0-9]+")
35: f_mant_re2 = Regexp.new("^-?[0-9]+")
36: if str =~ f_mant_re1
37: mant = "#{$&}"
38: rest = "#{$'}"
39: found = 1
40: elsif str =~ f_mant_re2
41: mant = "#{$&}" + ".0"
42: rest = "#{$'}"
43: found = 1
44: end
45: if found == 1 then
46: restsav = rest.clone
47: if rest != ""
48: f_sep_re = Regexp.new("^e|E")
49: if rest =~ f_sep_re
50: rest = "#{$'}"
51: restsav = rest.clone
52: f_expt_re = Regexp.new("^[-+]?[0-9]+")
53: if rest =~ f_expt_re
54: expt = "#{$&}"
55: rest = "#{$'}"
56: else
57: puts "ERROR 1 ap_in #{str}"
58: return "BAD"
59: end
60: else
61: expt = "0"
62: rest = restsav
63: end
64: else
65: expt = "0"
66: rest = restsav
67: end
68:
69: if rest != ""
70: f_sep_err_re = Regexp.new("^\\+/-")
71: if rest =~ f_sep_err_re
72: rest = "#{$'}"
73: f_mant_err_re = Regexp.new("^-?[0-9]+\.[0-9]+")
74: if rest =~ f_mant_err_re
75: mant_err = "#{$&}"
76: rest = "#{$'}"
77: if rest != ""
78: f_sep_re = Regexp.new("^e|E")
79: if rest =~ f_sep_re
80: rest = "#{$'}"
81: f_expt_err_re = Regexp.new("^[-+]?[0-9]+")
82: if rest =~ f_expt_err_re
83: expt_err = "#{$&}"
84: else
85: puts "ERROR 2 ap_in #{str}"
86: return "BAD"
87:
88: end
89: else
90: expt_err = "0"
91: end
92: else
93: expt_err = "0"
94: end
95: else
96: puts "ERROR 3 ap_in #{str}"
97: return "BAD"
98:
99: end
100: else
101: puts "ERROR 4 ap_in #{str}"
102: return "BAD"
103:
104: end
105: else
106: mant_err = "0"
107: expt_err = "0"
108: end
109: mant = mant.to_s
110: rmant = mant.reverse
111: if rmant =~ /^[0-9]*\./
112: cnt = "#{$&}".size
113: mant.sub!(/\./,"")
114: expt = expt.to_i - cnt + 1
115: elsif
116: rmant =~ /^[0-9]*/
117: cnt = "#{$&}".size
118: expt = expt.to_i - cnt + 1
119: else
120: puts "ap_in match B failed"
121: puts "mant = " + mant.to_s
122: end
123: if mant_err != "0"
124: rmant = mant_err.reverse
125: if rmant =~ /^[0-9]*\./
126: cnt = "#{$&}".size
127: mant_err.sub!(/\./,"")
128: expt_err = expt_err.to_i - cnt + 1
129: else
130: puts "ap_in match C failed"
131: end
132: end
133: # puts "Ap.rb mant = #{mant} expt = #{expt.to_s} err = #{mant_err} expt_err = #{expt_err}"
134: if mant_err == "0"
135: return Apfp.new(mant.to_i,expt,5,-NUM_DIGITS+expt+mant.to_s.size).norm
136: else
137: return Apfp.new(mant.to_i,expt,mant_err.to_i,expt_err).norm
138: end
139: else
140: puts "ERROR 5 ap_in #{str}"
141: return "BAD"
142:
143: end
144:
145: end
# File lib/rapfp/Ap.rb, line 181
181: def ap_max(a,b)
182: if a > b then
183: return a
184: else
185: return b
186: end
187: end
# File lib/rapfp/Ap.rb, line 174
174: def ap_min(a,b)
175: if a < b then
176: return a
177: else
178: return b
179: end
180: end