@@ -92,7 +92,12 @@ func TestIGRFdata_IGRF(t *testing.T) {
9292 }
9393 // Horizontal intensity
9494 allowed_abs_error := getMaxAllowedAbsoluteTolerance (tt .want .HorizontalIntensity )
95- compare = isClose (got .HorizontalIntensity , tt .want .HorizontalIntensity , allowed_relative_error , allowed_abs_error )
95+ is_close := isClose (got .HorizontalIntensity , tt .want .HorizontalIntensity , allowed_relative_error , allowed_abs_error )
96+ is_close_no_frac := noFracComparison (got .HorizontalIntensity , tt .want .HorizontalIntensity )
97+ compare = is_close || is_close_no_frac
98+ // For lat=-64.081, lon=135.866, alt=0, data=2021.5, horizontal intensity is 74.38, whereas reference value is 74.
99+ // Fortran rounds/truncates almost all values, therefore it's hard to use either relative or absolute tolerance.
100+ // noFracComparison compares a value with it's rounded/truncated value.
96101 if ! compare {
97102 t .Errorf ("IGRF() Horizontal intensity = %v, want %v" , got .HorizontalIntensity , tt .want .HorizontalIntensity )
98103 }
@@ -185,6 +190,17 @@ func getMaxAllowedAbsoluteTolerance(value float64) float64 {
185190 return abs_tol
186191}
187192
193+ func noFracComparison (val , ref float64 ) bool {
194+ // Since almost all values produces by Fortran are rounded/truncated, it's sometimes needed to compare
195+ // calculated value with rounded/truncated value. Rounding doesn't always work, as it's unclear whether Fortran values
196+ // are actually rounded or truncated.
197+ // Therefore values are truncated and compared to be within certain range.
198+ min := ref - 1.0
199+ max := ref + 1.0
200+ val_trunc := math .Floor ((val ))
201+ return min <= val && val_trunc <= max
202+ }
203+
188204func isClose (a , b , rel_tol , abs_tol float64 ) bool {
189205 abs_diff := math .Abs (a - b )
190206 a_abs := math .Abs (a )
0 commit comments