Last change
on this file since 117 was
117,
checked in by moretto, 11 years ago
|
ESAF version compilable on mac OS
|
File size:
1.3 KB
|
Line | |
---|
1 | #include "LensUtils.h" |
---|
2 | #include <globals.hh> |
---|
3 | #include <geomdefs.hh> |
---|
4 | |
---|
5 | using namespace G4FresnelLens; |
---|
6 | using std::swap; |
---|
7 | |
---|
8 | #define NUM_TOLERANCE 1.e-15 |
---|
9 | |
---|
10 | //__________________________________________________________________________________ |
---|
11 | int G4FresnelLens::SolveQuadratic(double a, double b, double c, double& x1, double& x2) |
---|
12 | { |
---|
13 | // |
---|
14 | // this function tries to avoid "catastrophic cancellation" due to subtraction |
---|
15 | // of almost equal numbers |
---|
16 | // |
---|
17 | if(a==0.0) { |
---|
18 | if(b==0.0) return 0; |
---|
19 | x1=x2=-c/b; |
---|
20 | return 1; |
---|
21 | } |
---|
22 | |
---|
23 | |
---|
24 | double b2a=b/a*0.5; |
---|
25 | double ca=c/a; |
---|
26 | double d=b2a*b2a-ca; |
---|
27 | if( d<0.0 ) return 0; |
---|
28 | if( d==0 ) { |
---|
29 | x1=x2=-b2a; |
---|
30 | return 1; |
---|
31 | } |
---|
32 | d=sqrt(d); |
---|
33 | if ( b2a<0 ) { |
---|
34 | x2=-b2a+d; |
---|
35 | x1=ca/x2; |
---|
36 | } |
---|
37 | else { |
---|
38 | x1=-b2a-d; |
---|
39 | x2=ca/x1; |
---|
40 | } |
---|
41 | return 2; |
---|
42 | } |
---|
43 | |
---|
44 | //__________________________________________________________________________________ |
---|
45 | int G4FresnelLens::BinarySearch(double* a, double x, int aleft, int aright){ |
---|
46 | // |
---|
47 | // adopted from Ltrace Lrt_bsearch |
---|
48 | // int Lrt_bsearch(double a[], double x, int left, int right) |
---|
49 | int center,left(aleft),right(aright); |
---|
50 | |
---|
51 | if(x<a[left]) return -1; |
---|
52 | if(x>a[right]) return -2; |
---|
53 | |
---|
54 | while(right-left>1){ |
---|
55 | center=(left+right)/2; |
---|
56 | if(x<a[center]) right=center; |
---|
57 | else left=center; |
---|
58 | } |
---|
59 | |
---|
60 | return left; |
---|
61 | } |
---|
Note: See
TracBrowser
for help on using the repository browser.