Fix #128102: Integer Math division has only float precision #128123

Merged
Jacques Lucke merged 4 commits from CharlieJolly/blender:fix128102 into main 2024-09-26 14:44:30 +02:00
Member

The additional division modes were implemented with float
division then rounding to match float > int conversion in
existing float to int node.

As pointed out in the bug report the precision here is limited.

This patch replaces the float division with integer math which
increases the precision to much higher numbers.
Divide Round by due to the way it is calculated has less precision
than Divide Floor and Divide Ceil.

The additional division modes were implemented with float division then rounding to match float > int conversion in existing float to int node. As pointed out in the bug report the precision here is limited. This patch replaces the float division with integer math which increases the precision to much higher numbers. Divide Round by due to the way it is calculated has less precision than Divide Floor and Divide Ceil.
Charlie Jolly added 1 commit 2024-09-25 16:00:10 +02:00
The additional division modes were implemented with float
division then rounding to match float > int conversion in
existing float to int node.

As pointed out in the bug report the precision here is limited.

This patch replaces the float division with integer math which
increases the precision to much higher numbers.
Divide Round by due to the way it is calculated has less precision
than Divide Floor and Divide Ceil.
Charlie Jolly added this to the Nodes & Physics project 2024-09-25 16:01:24 +02:00
Charlie Jolly requested review from Jacques Lucke 2024-09-25 16:02:33 +02:00
Jacques Lucke requested changes 2024-09-25 17:53:14 +02:00
Dismissed
@ -117,3 +117,3 @@
"Multiply", [](int a, int b) { return a * b; }, exec_preset);
static auto divide_fn = mf::build::SI2_SO<int, int, int>(
"Divide", [](int a, int b) { return math::safe_divide(a, b); }, exec_preset);
"Divide", [](int a, int b) { return (b != 0) ? a / b : 0; }, exec_preset);
Member

Isn't this the same as the math::safe_divide?

Isn't this the same as the `math::safe_divide`?
Author
Member

My mistake, I copied the function in full for testing.

My mistake, I copied the function in full for testing.
CharlieJolly marked this conversation as resolved
@ -130,1 +130,3 @@
[](int a, int b) { return int(math::round(math::safe_divide(float(a), float(b)))); },
[](int a, int b) {
const int c = abs(b);
return (a >= 0) ? math::safe_divide((2 * a + c), (2 * c)) * math::sign(b) :
Member

Would be good to extract that into a separate function in blenlib and to add some unit tests for it. (e.g. in BLI_math_base_safe_test.cc)

Where does this formular come from? Shouldn't it be possible to round based on the remainder?

Would be good to extract that into a separate function in blenlib and to add some unit tests for it. (e.g. in `BLI_math_base_safe_test.cc`) Where does this formular come from? Shouldn't it be possible to round based on the remainder?
Author
Member

This was derived from divide_round_i but fixed to be safe and handle negative inputs to match float output from math::round(math::safe_divide(a, b)).

I've added a comment to this affect and pulled into a separate function.

MINLINE int divide_round_i(int a, int b)
{
  return (2 * a + b) / (2 * b);
}

/* Derived from `divide_round_i` but fixed to be safe and handle negative inputs. */
static int safe_divide_round_i(const int a, const int b)
{
  const int c = math::abs(b);
  return (a >= 0) ? math::safe_divide((2 * a + c), (2 * c)) * math::sign(b) :
                    -math::safe_divide((2 * -a + c), (2 * c)) * math::sign(b);
}
This was derived from `divide_round_i` but fixed to be safe and handle negative inputs to match float output from `math::round(math::safe_divide(a, b))`. I've added a comment to this affect and pulled into a separate function. ```Cpp MINLINE int divide_round_i(int a, int b) { return (2 * a + b) / (2 * b); } /* Derived from `divide_round_i` but fixed to be safe and handle negative inputs. */ static int safe_divide_round_i(const int a, const int b) { const int c = math::abs(b); return (a >= 0) ? math::safe_divide((2 * a + c), (2 * c)) * math::sign(b) : -math::safe_divide((2 * -a + c), (2 * c)) * math::sign(b); } ```
Author
Member

I think it is easier to keep here rather than bloat blenlib but if you want it in blenlib can you advise which file it should go?

I think it is easier to keep here rather than bloat blenlib but if you want it in blenlib can you advise which file it should go?
Member

ok, fine with me, it would just be nice to have some kinds of test here, a regression test can be ok as well though.

ok, fine with me, it would just be nice to have some kinds of test here, a regression test can be ok as well though.
Charlie Jolly added 1 commit 2024-09-25 18:16:04 +02:00
Charlie Jolly added 2 commits 2024-09-25 18:53:52 +02:00
Tidy up divide round and add function and comment
All checks were successful
buildbot/vexp-code-patch-lint Build done.
buildbot/vexp-code-patch-linux-x86_64 Build done.
buildbot/vexp-code-patch-windows-amd64 Build done.
buildbot/vexp-code-patch-darwin-arm64 Build done.
buildbot/vexp-code-patch-darwin-x86_64 Build done.
buildbot/vexp-code-patch-coordinator Build done.
0d2c1bd0c9
Jacques Lucke approved these changes 2024-09-26 12:26:16 +02:00
Member

@blender-bot build

@blender-bot build
Author
Member

Test file showing precision fixes from this patch
image

Test file showing precision fixes from this patch <img width="467" alt="image" src="attachments/04999fa6-519f-4fb4-97d8-f8d18f358861">
Jacques Lucke merged commit c0a864aaa3 into main 2024-09-26 14:44:30 +02:00
Sign in to join this conversation.
No reviewers
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset System
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Code Documentation
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Viewport & EEVEE
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Asset Browser Project
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Module
Viewport & EEVEE
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Severity
High
Severity
Low
Severity
Normal
Severity
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No Assignees
2 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender#128123
No description provided.