From 6b345bb3ab13139cff8ca5b7fdc551110eaaddea Mon Sep 17 00:00:00 2001 From: Thomas White Date: Fri, 26 Feb 2010 23:41:40 +0100 Subject: Fix type conversions to avoid trouble with overflows etc. --- src/detector.c | 20 +++++++++++++++----- src/diffraction-gpu.c | 6 ++++++ src/pattern_sim.c | 3 +++ 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/detector.c b/src/detector.c index 2a4c6d10..f66ad525 100644 --- a/src/detector.c +++ b/src/detector.c @@ -74,14 +74,17 @@ void record_image(struct image *image, int do_poisson) for ( x=0; xwidth; x++ ) { for ( y=0; yheight; y++ ) { - int counts; + double counts; double cf; double intensity, sa; double pix_area, Lsq; double dsq, proj_area; struct panel *p; - intensity = image->data[x + image->width*y]; + intensity = (double)image->data[x + image->width*y]; + if ( isinf(intensity) ) { + ERROR("Infinity at %i,%i\n", x, y); + } p = find_panel(&image->det, x, y); @@ -102,13 +105,20 @@ void record_image(struct image *image, int do_poisson) if ( do_poisson ) { counts = poisson_noise(intensity * ph_per_e * sa * DQE); } else { - double rounded; cf = intensity * ph_per_e * sa * DQE; - rounded = rint(cf); - counts = (int)rounded; + counts = rint(cf); + if ( counts < 0.0 ) { + ERROR("Negative at %i,%i %f\n", x, y, counts); + } } image->data[x + image->width*y] = counts * DETECTOR_GAIN; + if ( isinf(image->data[x+image->width*y]) ) { + ERROR("Processed infinity at %i,%i\n", x, y); + } + if ( image->data[x+image->width*y] < 0.0 ) { + ERROR("Processed negative at %i,%i %f\n", x, y, counts); + } } progress_bar(x, image->width-1, "Post-processing"); diff --git a/src/diffraction-gpu.c b/src/diffraction-gpu.c index a2d1c47a..851ed70c 100644 --- a/src/diffraction-gpu.c +++ b/src/diffraction-gpu.c @@ -239,6 +239,12 @@ void get_diffraction_gpu(struct gpu_context *gctx, struct image *image, float val, tt; val = diff_ptr[x + image->width*y]; + if ( isinf(val) ) { + ERROR("Extracting infinity at %i,%i\n", x, y); + } + if ( val < 0.0 ) { + ERROR("Extracting negative at %i,%i\n", x, y); + } tt = tt_ptr[x + image->width*y]; image->data[x + image->width*y] = val; diff --git a/src/pattern_sim.c b/src/pattern_sim.c index 91100426..59e5adb5 100644 --- a/src/pattern_sim.c +++ b/src/pattern_sim.c @@ -307,6 +307,9 @@ int main(int argc, char *argv[]) for ( x=0; x