{"record":{"id":"a334235c57d362fc","repo":"Yalantis/uCrop","slug":"displacement-invalid-specified-precision-g-sh","errorCode":null,"errorMessage":"displacement(): Invalid specified precision %g (should be >=0)","messagePattern":"displacement\\(\\): Invalid specified precision %g \\(should be >=0\\)","errorType":"validation","errorClass":"CImgArgumentException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":45966,"sourceCode":"        move_to(*this);\n    }\n\n    //! Estimate displacement field between two images \\newinstance.\n    CImg<floatT> get_displacement(const CImg<T>& reference,\n                                  const float smoothness=0.1f, const float precision=5.f,\n                                  const unsigned int nb_scales=0, const unsigned int iteration_max=1000,\n                                  const bool is_forward=false,\n                                  const CImg<floatT>& guide=CImg<floatT>::const_empty()) const {\n      if (is_empty() || !reference) return +*this;\n      if (!is_sameXYZC(reference))\n        throw CImgArgumentException(_cimg_instance\n                                    \"displacement(): Instance and reference image (%u,%u,%u,%u,%p) have \"\n                                    \"different dimensions.\",\n                                    cimg_instance,\n                                    reference._width,reference._height,reference._depth,reference._spectrum,\n                                    reference._data);\n      if (precision<0)\n        throw CImgArgumentException(_cimg_instance\n                                    \"displacement(): Invalid specified precision %g \"\n                                    \"(should be >=0)\",\n                                    cimg_instance,\n                                    precision);\n\n      const bool is_3d = reference._depth>1;\n      const unsigned int spectrum_U = is_3d?3:2;\n\n      if (guide &&\n          (guide._width!=_width || guide._height!=_height || guide._depth!=_depth || guide._spectrum<spectrum_U))\n        throw CImgArgumentException(_cimg_instance\n                                    \"displacement(): Specified guide (%u,%u,%u,%u,%p) \"\n                                    \"has invalid dimensions.\",\n                                    cimg_instance,\n                                    guide._width,guide._height,guide._depth,guide._spectrum,guide._data);\n      const float\n        scale_factor = 2.f,\n        abs_smoothness = cimg::abs(smoothness),","sourceCodeStart":45948,"sourceCodeEnd":45984,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L45948-L45984","documentation":"displacement() takes a precision parameter that controls the convergence threshold of the iterative optical-flow solver. Negative values are meaningless for this stopping criterion, so the library throws this CImgArgumentException when precision<0.","triggerScenarios":"Calling displacement() with a negative precision argument, e.g. displacement(ref, smoothness, -1.0f), typically from a miscalculation, a sign error, or an unvalidated user/config value.","commonSituations":"Config files where precision was meant as an offset or tolerance and a negative value was entered, variables initialized with -1 as a sentinel and passed unmodified, or unit confusion (pixels vs subpixels).","solutions":["Pass a positive precision (the default is 5.0f); lower values give more precise but slower convergence","Clamp the value before calling: if (precision < 0) precision = 0 (or a sensible default)","Validate user/config input with a range check (precision >= 0) at parse time","Check code that computes precision dynamically for sign errors"],"exampleFix":"// before\nimg.displacement(ref, 0.1f, precision /* -1.f sentinel */);\n// after\nimg.displacement(ref, 0.1f, precision < 0 ? 5.0f : precision);","handlingStrategy":"validation","validationCode":"float precision = cfg.precision;\nif (precision < 0) precision = 5.0f; // library default\nCImg<float> flow = img.displacement(reference, smoothness, precision);","typeGuard":null,"tryCatchPattern":"try {\n  CImg<float> flow = img.displacement(reference, smoothness, precision);\n} catch (CImgArgumentException& e) {\n  std::fprintf(stderr, \"precision must be >=0: %s\\n\", e.what());\n  CImg<float> flow = img.displacement(reference); // default precision=5\n}","preventionTips":["Clamp precision to >=0 right after reading it from config/user input","Avoid -1 sentinel values flowing into numeric parameters","Remember lower precision values mean slower but more accurate convergence","Use named defaults (5.0f) instead of ad-hoc magic numbers"],"tags":["cimg","argument-validation","parameter-range"],"backgroundTag":"argument-out-of-range","analyzedSha":"f788b534b48c144edf786c8cddbf0e029e637804","analyzedAt":"2026-09-08T08:36:04.887Z","contentChangedAt":"2026-09-08T08:36:04.887Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}