Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

midpoint(x,x) != x #1

Open
837951602 opened this issue Dec 6, 2023 · 9 comments
Open

midpoint(x,x) != x #1

837951602 opened this issue Dec 6, 2023 · 9 comments

Comments

@837951602
Copy link

https://gcc.godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1DIApACYAQuYukl9ZATwDKjdAGFUtAK4sGISQDMpK4AMngMmAByPgBGmMT%2B0gAOqAqETgwe3r7%2BQSlpjgJhEdEscQmS0naYDhlCBEzEBFk%2BfoG2mPaFDPWNBMVRsfGJtg1NLTntCmP94YNlw5UAlLaoXsTI7BzmAXhUDFhUANQAsgCSACLKAPJnkQAqAPoAEiYaAII7h3Onlzd3T2eRy4b0%2BHx24WQ3iwRxMATcyBYTAICDh2FBEIYUK8MLhCOm%2BEEaIxH2YbAUSSYmyOBIxAHYrOCPkcjgRMCwkgY2XiCABPJKMViYI6PM5EgLo5kso5oBjTTCqJLEGkEdAgECCmL0R57R4EPEE9V4BQ6wSYYDEMSPABueNF4slYIAnG8XRondLzAA2b1HMBgQ0gY2PBRCm12sUEUhHGKoTxo6P2/US0HSo4sPDoFLhAgQJMix5MRORgsxJZHBioBWbJL6qUskwM1Npo5eNJGEVyEtwi4qtUgJEAa0wjy8crwwAi6D1EYdcMZ7wA9AAqZtp2XTI5MLxEAteJiwgK9x5dwR5wtLedr6UbghbneoPcxQ/H0%2B5x6PMtX%2Bs3gSb7e7h%2BWC0C%2BRznl4z5onuB5wgAYrCljgc%2BAC00HliARzIeBB6oR%2BEFLJeAQLi2RzEJgBDrAwIqRthCEWAWwFHIuRxmIRC7Lou14smRFHEFRSZ5m%2B55MOW1hgSeNEfmWmGd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0NU7odTZglCGAkKpMwildL6OUjpEgVgKEXpsQZt8OBj1IBPKeM8OBHFUCXL0yEvSSBlAYDsWcE67y4GBWB8DugKHLBAXAhASAIQCFwJYvByFaAIqQbeZgnQJydCXLgXBS5cGTl6LgTpT4rPGffKZj9Zkvzfh/RhX8YCIBAGsAgSQdxAIgCAsBu02CcAWUslZazIHAjpFshOOyIB7PiAcq5wR8BEDiXofgNjRDiAcTSpxKh1CCLcaQUxloki6PGZM6ZUTODXB3PCu8qBjgYuWasiBGy8XbN2XAkloSjkeH/vQTm%2BdLnXM/lvEAh8E7JzpF6Q1h8S50kkE9R5AKH6CJBbYMFNyh6kG/tCognhEWNEMZwZCyFHBsDmhETCLBkDwp7MwRwYjeDuoUMoQwnQhAIFQKYvlpBkWqtRVscVWKpXAFxfiwlxKmiKt4JgClJBMzUtkLY%2Bl0hGWKGZa4vQLABBUlQCcylzxYEWEwIwd4D4LGMGLVYxtzA0CtpIH2ieq8h3NvqISKxBJwjP1GISaNERaBxoTRO65cZaBnDVLQWg1oWDIUDciYgY5Bzss5dykeEyrUzM4Puw9x7RBgS0ee8sGbJXrOzZs2VRL5UFr/Ec0daqLnRmVaA1V5yNXgtudqvFJdJAaDTlIAIycS4YYCHSIIN7AVJpta/d%2B9q7k3rMHe/lHBNUQpWDEtIzhJBAA

@emsr
Copy link
Owner

emsr commented Dec 6, 2023

(gdb) p K
$2 = 4.4501477170144023e-308
(gdb) p K/2+K/2
$3 = 4.4501477170144028e-308
(gdb) p (K+K)/2
$4 = 4.4501477170144023e-308

The logic takes the route with both variables normalized.

@emsr
Copy link
Owner

emsr commented Dec 6, 2023

https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1DIApACYAQuYukl9ZATwDKjdAGFUtAK4sGe1wAyeAyYAHI%2BAEaYxCAArACcpAAOqAqETgwe3r56KWmOAkE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link
Author

If differing by eps is allowed then a*.5+b*.5 is enough, no need your midpoint

@837951602
Copy link
Author

differing by eps/2 is unavoidable

@emsr
Copy link
Owner

emsr commented Dec 6, 2023

So K/2+K/2 != (K+K)/2
I've checked in the debugger.
I've tried -fno-fast-math and get the same results.

We may not be able to do better that (a+b)/2.

I'll read the paper and see if there was a rationale for the existing implementation.

@emsr
Copy link
Owner

emsr commented Dec 6, 2023

These values are much smaller than epsilon though. These are large compared to the denormalization point which for doubles is 4.9406564584124654e-324.

It's puzzling what the arithmetic does though. This is some bug but below the level of this library.
I might just do (a+b)/2 here. But this makes the component kind of silly though.

I might file a bug report and see if any language people have any ideas.

Actually, the difference might be on the order of denorm_min.

@837951602
Copy link
Author

Here by eps I mean [the smallest value larger than x]-x. (a+b)/2 should work on this magnitude

@evilguest
Copy link

Integral midpoint is broken due to the wrong parentheses.
midpoint(42, 42) yields 63.

@emsr
Copy link
Owner

emsr commented Apr 26, 2024

Ugh. That was bad. Sorry.
I did a push and added a test to fix that.
I also tweaked the floating point to better work with subnormal numbers hopefully.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants