nec2++ 2.1.1
BaseInput.h
1/*
2 Copyright (C) 2004 Timothy C.A. Molteno
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17*/
18#pragma once
19
20#include "math_util.h"
21
22#include <algorithm>
23#include <cmath>
24#include <fstream>
25#include <sstream>
26
27/* Relative difference of two values, normalised by the larger magnitude.
28 The scale test must use magnitudes: comparing signed values against the
29 1e-8 floor treats any two negative numbers as "effectively zero". */
30nec_float diff(nec_float a, nec_float b)
31{
32 if (a == b)
33 return 0;
34
35 nec_float scale = std::max(std::fabs(a), std::fabs(b));
36 if (scale < 1e-8)
37 return 0;
38
39 nec_float ret = std::fabs(a - b) / scale;
40 if (ret > 1e-2)
41 {
42 cout << " diff(" << a << "," << b << ") = " << ret << endl;
43 }
44 return ret;
45}
46
47nec_float diff(nec_complex a, nec_complex b)
48{
49 if (a == b)
50 return 0;
51
52 nec_float scale = std::max(abs(a), abs(b));
53 if (scale < 1e-8)
54 return 0;
55
56 nec_float ret = abs(a - b) / scale;
57 if (ret > 1e-2)
58 {
59 stringstream ss;
60 cout << " diff(" << a << "," << b << ") = " << ret << endl;
61 }
62 return ret;
63}
64
65class BaseInput
66{
67public:
68 BaseInput(const std::string& filename)
69 : m_filename(filename),
70 m_stream(filename.c_str())
71 {
72 }
73
74private:
75 char linec[512];
76
77
78protected:
79
80 std::string m_filename;
81 std::ifstream m_stream;
82
83 string readline()
84 {
85 m_stream.getline(&linec[0],512);
86 return string(&linec[0]);
87 }
88
89 double read_sci(std::istream& is)
90 {
91 double x = 0.0; /* zero on extraction failure (was returned uninitialized) */
92 is.setf(ios_base::skipws);
93 is >> x;
94 return x;
95 }
96
97 double read_fixed(std::istream& is)
98 {
99 double x = 0.0; /* zero on extraction failure (was returned uninitialized) */
100 is.setf(ios_base::skipws);
101 is >> x;
102 return x;
103 }
104};