Forum > General

three-way comparison

(1/5) > >>

SonnyBoyXXl:
Working on DirectX Math 3.18 i found a very strange C++ code segment.

--- Code: C  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---#if (__cplusplus >= 202002L)        bool operator == (const XMFLOAT2&) const = default;        auto operator <=> (const XMFLOAT2&) const = default;#endif
The operator <=> is called a three-way comparison operator.
It returns -1 if a<b, returns 0 if a=b and returns 1 if b>a. So far so good. But how do I implement such operator for a type in pascal. I  think there is not a predefined operator at the moment?

Red_prig:
There are several different ways, for example I did this:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---Result:=Integer(key1>key2)-Integer(key1<key2);

Zvoni:

--- Quote from: Red_prig on November 28, 2022, 09:00:23 pm ---There are several different ways, for example I did this:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---Result:=Integer(key1>key2)-Integer(key1<key2);
--- End quote ---
You‘re implying that True „equals“ to 1 (the integer-cast)
Is this always the case? Or might True result to -1?

Josh:
would sign function not work?
https://www.freepascal.org/docs-html/rtl/math/sign.html

In computer science that is called ternary logic. I have implemented it here, some time ago and in some forms.
Look at https://forum.lazarus.freepascal.org/index.php/topic,41144.msg295325.html#msg295325
(The Rosetta code entry is also written by me)

[#] Next page

Go to full version