Topic: Function to compute the absolute value of any
Author: burningorca@gmail.com
Date: Wed, 3 Oct 2018 22:32:38 -0700 (PDT)
Raw View
------=_Part_94_1847078565.1538631158399
Content-Type: multipart/alternative;
boundary="----=_Part_95_213848872.1538631158399"
------=_Part_95_213848872.1538631158399
Content-Type: text/plain; charset="UTF-8"
I would like to add a function which gets any integral value and computes
the positive value of it to the standard. Something like std::abs, but
instead of returning the same type, it should return the unsigned type, in
order to get around the undefined behaviour when passing
std::numeric_limits<SignedType>::min() as an argument.
#include <type_traits>
#include <cstdint>
#include <limits>
#include <cmath>
template<typename T>
concept bool Integral = std::is_integral<T>::value;
auto uabs(const Integral s)
{
using UIntegral_t = std::make_unsigned_t<std::decay_t<decltype(s)>>;
if( s >= 0 )
return static_cast<UIntegral_t>(s);
else
return static_cast<UIntegral_t>(~s + 1);
}
int main(void)
{
auto i = static_cast<std::uint64_t>(std::abs(1.0 * std::numeric_limits<
std::int64_t>::min())); // Is not an option because of precision errors.
return uabs(std::numeric_limits<std::int8_t>::min());
}
We could call it std::uabs or std::make_positive.
If there exists already something like this in the standard, please feel
free to tell me!
And if not maybe you can come up with a better name.
--
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/3f8200be-918a-44a0-a412-6f8f43da1c51%40isocpp.org.
------=_Part_95_213848872.1538631158399
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr"><div>I would like to add a function which gets any integra=
l value and computes the positive value of it to the standard. Something li=
ke std::abs, but instead of returning the same type, it should return the u=
nsigned type, in order to get around the undefined behaviour when passing s=
td::numeric_limits<SignedType>::min() as an argument.</div><div><br><=
/div><div><div><div style=3D"background-color: rgb(250, 250, 250); border-c=
olor: rgb(187, 187, 187); border-style: solid; border-width: 1px; overflow-=
wrap: break-word;" class=3D"prettyprint"><code class=3D"prettyprint"><div c=
lass=3D"subprettyprint"><span style=3D"color: #800;" class=3D"styled-by-pre=
ttify">#include</span><span style=3D"color: #000;" class=3D"styled-by-prett=
ify"> </span><span style=3D"color: #080;" class=3D"styled-by-prettify"><=
type_traits></span><span style=3D"color: #000;" class=3D"styled-by-prett=
ify"><br></span><span style=3D"color: #800;" class=3D"styled-by-prettify">#=
include</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </=
span><span style=3D"color: #080;" class=3D"styled-by-prettify"><cstdint&=
gt;</span><span style=3D"color: #000;" class=3D"styled-by-prettify"><br></s=
pan><span style=3D"color: #800;" class=3D"styled-by-prettify">#include</spa=
n><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span s=
tyle=3D"color: #080;" class=3D"styled-by-prettify"><limits></span><sp=
an style=3D"color: #000;" class=3D"styled-by-prettify"><br></span><span sty=
le=3D"color: #800;" class=3D"styled-by-prettify">#include</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color=
: #080;" class=3D"styled-by-prettify"><cmath></span><span style=3D"co=
lor: #000;" class=3D"styled-by-prettify"><br><br></span><span style=3D"colo=
r: #008;" class=3D"styled-by-prettify">template</span><span style=3D"color:=
#660;" class=3D"styled-by-prettify"><</span><span style=3D"color: #008;=
" class=3D"styled-by-prettify">typename</span><span style=3D"color: #000;" =
class=3D"styled-by-prettify"> T</span><span style=3D"color: #660;" class=3D=
"styled-by-prettify">></span><span style=3D"color: #000;" class=3D"style=
d-by-prettify"><br></span><span style=3D"color: #008;" class=3D"styled-by-p=
rettify">concept</span><span style=3D"color: #000;" class=3D"styled-by-pret=
tify"> </span><span style=3D"color: #008;" class=3D"styled-by-prettify">boo=
l</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><=
span style=3D"color: #606;" class=3D"styled-by-prettify">Integral</span><sp=
an style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=
=3D"color: #660;" class=3D"styled-by-prettify">=3D</span><span style=3D"col=
or: #000;" class=3D"styled-by-prettify"> std</span><span style=3D"color: #6=
60;" class=3D"styled-by-prettify">::</span><span style=3D"color: #000;" cla=
ss=3D"styled-by-prettify">is_integral</span><span style=3D"color: #660;" cl=
ass=3D"styled-by-prettify"><</span><span style=3D"color: #000;" class=3D=
"styled-by-prettify">T</span><span style=3D"color: #660;" class=3D"styled-b=
y-prettify">>::</span><span style=3D"color: #000;" class=3D"styled-by-pr=
ettify">value</span><span style=3D"color: #660;" class=3D"styled-by-prettif=
y">;</span><span style=3D"color: #000;" class=3D"styled-by-prettify"><br><b=
r></span><span style=3D"color: #008;" class=3D"styled-by-prettify">auto</sp=
an><span style=3D"color: #000;" class=3D"styled-by-prettify"> uabs</span><s=
pan style=3D"color: #660;" class=3D"styled-by-prettify">(</span><span style=
=3D"color: #008;" class=3D"styled-by-prettify">const</span><span style=3D"c=
olor: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color: #60=
6;" class=3D"styled-by-prettify">Integral</span><span style=3D"color: #000;=
" class=3D"styled-by-prettify"> s</span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">)</span><span style=3D"color: #000;" class=3D"style=
d-by-prettify"><br></span><span style=3D"color: #660;" class=3D"styled-by-p=
rettify">{</span><span style=3D"color: #000;" class=3D"styled-by-prettify">=
<br>=C2=A0 =C2=A0 </span><span style=3D"color: #008;" class=3D"styled-by-pr=
ettify">using</span><span style=3D"color: #000;" class=3D"styled-by-prettif=
y"> </span><span style=3D"color: #606;" class=3D"styled-by-prettify">UInteg=
ral_t</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </sp=
an><span style=3D"color: #660;" class=3D"styled-by-prettify">=3D</span><spa=
n style=3D"color: #000;" class=3D"styled-by-prettify"> std</span><span styl=
e=3D"color: #660;" class=3D"styled-by-prettify">::</span><span style=3D"col=
or: #000;" class=3D"styled-by-prettify">make_unsigned_t</span><span style=
=3D"color: #660;" class=3D"styled-by-prettify"><</span><span style=3D"co=
lor: #000;" class=3D"styled-by-prettify">std</span><span style=3D"color: #6=
60;" class=3D"styled-by-prettify">::</span><span style=3D"color: #000;" cla=
ss=3D"styled-by-prettify">decay_t</span><span style=3D"color: #660;" class=
=3D"styled-by-prettify"><</span><span style=3D"color: #008;" class=3D"st=
yled-by-prettify">decltype</span><span style=3D"color: #660;" class=3D"styl=
ed-by-prettify">(</span><span style=3D"color: #000;" class=3D"styled-by-pre=
ttify">s</span><span style=3D"color: #660;" class=3D"styled-by-prettify">)&=
gt;>;</span><span style=3D"color: #000;" class=3D"styled-by-prettify"><b=
r>=C2=A0 =C2=A0 </span><span style=3D"color: #008;" class=3D"styled-by-pret=
tify">if</span><span style=3D"color: #660;" class=3D"styled-by-prettify">(<=
/span><span style=3D"color: #000;" class=3D"styled-by-prettify"> s </span><=
span style=3D"color: #660;" class=3D"styled-by-prettify">>=3D</span><spa=
n style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=
=3D"color: #066;" class=3D"styled-by-prettify">0</span><span style=3D"color=
: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color: #660;" =
class=3D"styled-by-prettify">)</span><span style=3D"color: #000;" class=3D"=
styled-by-prettify"><br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 </span><span style=3D"c=
olor: #008;" class=3D"styled-by-prettify">return</span><span style=3D"color=
: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color: #008;" =
class=3D"styled-by-prettify">static_cast</span><span style=3D"color: #660;"=
class=3D"styled-by-prettify"><</span><span style=3D"color: #606;" class=
=3D"styled-by-prettify">UIntegral_t</span><span style=3D"color: #660;" clas=
s=3D"styled-by-prettify">>(</span><span style=3D"color: #000;" class=3D"=
styled-by-prettify">s</span><span style=3D"color: #660;" class=3D"styled-by=
-prettify">);</span><span style=3D"color: #000;" class=3D"styled-by-prettif=
y"><br>=C2=A0 =C2=A0 </span><span style=3D"color: #008;" class=3D"styled-by=
-prettify">else</span><span style=3D"color: #000;" class=3D"styled-by-prett=
ify"><br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 </span><span style=3D"color: #008;" cl=
ass=3D"styled-by-prettify">return</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify"> </span><span style=3D"color: #008;" class=3D"style=
d-by-prettify">static_cast</span><span style=3D"color: #660;" class=3D"styl=
ed-by-prettify"><</span><span style=3D"color: #606;" class=3D"styled-by-=
prettify">UIntegral_t</span><span style=3D"color: #660;" class=3D"styled-by=
-prettify">>(~</span><span style=3D"color: #000;" class=3D"styled-by-pre=
ttify">s </span><span style=3D"color: #660;" class=3D"styled-by-prettify">+=
</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><s=
pan style=3D"color: #066;" class=3D"styled-by-prettify">1</span><span style=
=3D"color: #660;" class=3D"styled-by-prettify">);</span><span style=3D"colo=
r: #000;" class=3D"styled-by-prettify"><br></span><span style=3D"color: #66=
0;" class=3D"styled-by-prettify">}</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify"><br><br></span><span style=3D"color: #008;" class=
=3D"styled-by-prettify">int</span><span style=3D"color: #000;" class=3D"sty=
led-by-prettify"> main</span><span style=3D"color: #660;" class=3D"styled-b=
y-prettify">(</span><span style=3D"color: #008;" class=3D"styled-by-prettif=
y">void</span><span style=3D"color: #660;" class=3D"styled-by-prettify">)</=
span><span style=3D"color: #000;" class=3D"styled-by-prettify"><br></span><=
span style=3D"color: #660;" class=3D"styled-by-prettify">{</span><span styl=
e=3D"color: #000;" class=3D"styled-by-prettify"><br>=C2=A0 =C2=A0 </span><s=
pan style=3D"color: #008;" class=3D"styled-by-prettify">auto</span><span st=
yle=3D"color: #000;" class=3D"styled-by-prettify"> i </span><span style=3D"=
color: #660;" class=3D"styled-by-prettify">=3D</span><span style=3D"color: =
#000;" class=3D"styled-by-prettify"> </span><span style=3D"color: #008;" cl=
ass=3D"styled-by-prettify">static_cast</span><span style=3D"color: #660;" c=
lass=3D"styled-by-prettify"><</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify">std</span><span style=3D"color: #660;" class=3D"sty=
led-by-prettify">::</span><span style=3D"color: #000;" class=3D"styled-by-p=
rettify">uint64_t</span><span style=3D"color: #660;" class=3D"styled-by-pre=
ttify">>(</span><span style=3D"color: #000;" class=3D"styled-by-prettify=
">std</span><span style=3D"color: #660;" class=3D"styled-by-prettify">::</s=
pan><span style=3D"color: #000;" class=3D"styled-by-prettify">abs</span><sp=
an style=3D"color: #660;" class=3D"styled-by-prettify">(</span><span style=
=3D"color: #066;" class=3D"styled-by-prettify">1.0</span><span style=3D"col=
or: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color: #660;=
" class=3D"styled-by-prettify">*</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify"> std</span><span style=3D"color: #660;" class=3D"st=
yled-by-prettify">::</span><span style=3D"color: #000;" class=3D"styled-by-=
prettify">numeric_limits</span><span style=3D"color: #660;" class=3D"styled=
-by-prettify"><</span><span style=3D"color: #000;" class=3D"styled-by-pr=
ettify">std</span><span style=3D"color: #660;" class=3D"styled-by-prettify"=
>::</span><span style=3D"color: #000;" class=3D"styled-by-prettify">int64_t=
</span><span style=3D"color: #660;" class=3D"styled-by-prettify">>::</sp=
an><span style=3D"color: #000;" class=3D"styled-by-prettify">min</span><spa=
n style=3D"color: #660;" class=3D"styled-by-prettify">()));</span><span sty=
le=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"col=
or: #800;" class=3D"styled-by-prettify">// Is not an option because of prec=
ision errors.</span><span style=3D"color: #000;" class=3D"styled-by-prettif=
y"><br>=C2=A0 =C2=A0 </span><span style=3D"color: #008;" class=3D"styled-by=
-prettify">return</span><span style=3D"color: #000;" class=3D"styled-by-pre=
ttify"> uabs</span><span style=3D"color: #660;" class=3D"styled-by-prettify=
">(</span><span style=3D"color: #000;" class=3D"styled-by-prettify">std</sp=
an><span style=3D"color: #660;" class=3D"styled-by-prettify">::</span><span=
style=3D"color: #000;" class=3D"styled-by-prettify">numeric_limits</span><=
span style=3D"color: #660;" class=3D"styled-by-prettify"><</span><span s=
tyle=3D"color: #000;" class=3D"styled-by-prettify">std</span><span style=3D=
"color: #660;" class=3D"styled-by-prettify">::</span><span style=3D"color: =
#000;" class=3D"styled-by-prettify">int8_t</span><span style=3D"color: #660=
;" class=3D"styled-by-prettify">>::</span><span style=3D"color: #000;" c=
lass=3D"styled-by-prettify">min</span><span style=3D"color: #660;" class=3D=
"styled-by-prettify">());</span><span style=3D"color: #000;" class=3D"style=
d-by-prettify"><br></span><span style=3D"color: #660;" class=3D"styled-by-p=
rettify">}</span></div></code></div></div></div><div><br></div><div>We coul=
d call it std::uabs or std::make_positive.<br>If there exists already somet=
hing like this in the standard, please feel free to tell me!</div><div>And =
if not maybe you can come up with a better name.</div></div>
<p></p>
-- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/3f8200be-918a-44a0-a412-6f8f43da1c51%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/3f8200be-918a-44a0-a412-6f8f43da1c51=
%40isocpp.org</a>.<br />
------=_Part_95_213848872.1538631158399--
------=_Part_94_1847078565.1538631158399--
.
Author: John McFarlane <john@mcfarlane.name>
Date: Fri, 12 Oct 2018 23:17:22 +0100
Raw View
--0000000000004b97fe05780f74cc
Content-Type: text/plain; charset="UTF-8"
It's a neat idea that solves a problem, but any addition to the standard
library should be of overall benefit to users.
Some reasons why I think there may be a lack of interest in this suggestion:
- It's quite specific: it solves a single edge case only.
- When that case arises, it's highly likely that the user already has other
problems: it's often unwise to rely on a type to store its most negative
number and there are freely-available tools for trapping the associated UB.
- A better solution is usually to chose a wider type. This may be a less
efficient solution in some cases but it's easier to teach and more
generally applicable.
- Changing from a `signed` to an `unsigned` type has more consequences than
the keywords let on. Unsigned integers behave differently in a number of
ways and are often less appropriate for representing the things for which
signed integers are well suited (and vice versa).
- I worry that users would be tempted to favour `std::uabs` over the
existing `std::abs` interface which is more generic and more easy to reason
about.
Regards,
John
On Thu, Oct 4, 2018, 06:32 <burningorca@gmail.com> wrote:
> I would like to add a function which gets any integral value and computes
> the positive value of it to the standard. Something like std::abs, but
> instead of returning the same type, it should return the unsigned type, in
> order to get around the undefined behaviour when passing
> std::numeric_limits<SignedType>::min() as an argument.
>
> #include <type_traits>
> #include <cstdint>
> #include <limits>
> #include <cmath>
>
> template<typename T>
> concept bool Integral = std::is_integral<T>::value;
>
> auto uabs(const Integral s)
> {
> using UIntegral_t = std::make_unsigned_t<std::decay_t<decltype(s)>>;
> if( s >= 0 )
> return static_cast<UIntegral_t>(s);
> else
> return static_cast<UIntegral_t>(~s + 1);
> }
>
> int main(void)
> {
> auto i = static_cast<std::uint64_t>(std::abs(1.0 * std::numeric_limits
> <std::int64_t>::min())); // Is not an option because of precision errors.
> return uabs(std::numeric_limits<std::int8_t>::min());
> }
>
> We could call it std::uabs or std::make_positive.
> If there exists already something like this in the standard, please feel
> free to tell me!
> And if not maybe you can come up with a better name.
>
> --
> You received this message because you are subscribed to the Google Groups
> "ISO C++ Standard - Future Proposals" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to std-proposals+unsubscribe@isocpp.org.
> To post to this group, send email to std-proposals@isocpp.org.
> To view this discussion on the web visit
> https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/3f8200be-918a-44a0-a412-6f8f43da1c51%40isocpp.org
> <https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/3f8200be-918a-44a0-a412-6f8f43da1c51%40isocpp.org?utm_medium=email&utm_source=footer>
> .
>
--
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CABPJVnS%2BUYT9Q-6X3Y_t043%3D%3DND38cyA5048c1HjbVkX8q3cEw%40mail.gmail.com.
--0000000000004b97fe05780f74cc
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr"><div>It's a neat idea that solves a problem, but any a=
ddition to the standard library should be of overall benefit to users.<br><=
/div><div><br></div><div>Some reasons why I think there may be a lack of in=
terest in this suggestion:</div><div>- It's quite specific: it solves a=
single edge case only.</div><div>- When that case arises, it's highly =
likely that the user already has other problems: it's often unwise to r=
ely on a type to store its most negative number and there are freely-availa=
ble tools for trapping the associated UB.<br></div><div>- A better solution=
is usually to chose a wider type. This may be a less efficient solution in=
some cases but it's easier to teach and more generally applicable.<br>=
</div><div>- Changing from a `signed` to an `unsigned` type has more conseq=
uences than the keywords let on. Unsigned integers behave differently in a =
number of ways and are often less appropriate for representing the things f=
or which signed integers are well suited (and vice versa). <br></div><div>-=
I worry that users would be tempted to favour `std::uabs` over the existin=
g `std::abs` interface which is more generic and more easy to reason about.=
<br></div><div><br></div><div>Regards,<br></div><div>John<br></div><br><div=
dir=3D"ltr"><div class=3D"gmail_quote"><div dir=3D"ltr">On Thu, Oct 4, 201=
8, 06:32 <<a href=3D"mailto:burningorca@gmail.com" target=3D"_blank">bu=
rningorca@gmail.com</a>> wrote:<br></div><blockquote class=3D"gmail_quot=
e" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">=
<div dir=3D"ltr"><div>I would like to add a function which gets any integra=
l value and computes the positive value of it to the standard. Something li=
ke std::abs, but instead of returning the same type, it should return the u=
nsigned type, in order to get around the undefined behaviour when passing s=
td::numeric_limits<SignedType>::min() as an argument.</div><div><br><=
/div><div><div><div style=3D"background-color:rgb(250,250,250);border-color=
:rgb(187,187,187);border-style:solid;border-width:1px" class=3D"m_284765216=
7001302833m_-1524075392614256550m_-545705664937522652prettyprint"><code cla=
ss=3D"m_2847652167001302833m_-1524075392614256550m_-545705664937522652prett=
yprint"><div class=3D"m_2847652167001302833m_-1524075392614256550m_-5457056=
64937522652subprettyprint"><span style=3D"color:#800" class=3D"m_2847652167=
001302833m_-1524075392614256550m_-545705664937522652styled-by-prettify">#in=
clude</span><span style=3D"color:#000" class=3D"m_2847652167001302833m_-152=
4075392614256550m_-545705664937522652styled-by-prettify"> </span><span styl=
e=3D"color:#080" class=3D"m_2847652167001302833m_-1524075392614256550m_-545=
705664937522652styled-by-prettify"><type_traits></span><span style=3D=
"color:#000" class=3D"m_2847652167001302833m_-1524075392614256550m_-5457056=
64937522652styled-by-prettify"><br></span><span style=3D"color:#800" class=
=3D"m_2847652167001302833m_-1524075392614256550m_-545705664937522652styled-=
by-prettify">#include</span><span style=3D"color:#000" class=3D"m_284765216=
7001302833m_-1524075392614256550m_-545705664937522652styled-by-prettify"> <=
/span><span style=3D"color:#080" class=3D"m_2847652167001302833m_-152407539=
2614256550m_-545705664937522652styled-by-prettify"><cstdint></span><s=
pan style=3D"color:#000" class=3D"m_2847652167001302833m_-15240753926142565=
50m_-545705664937522652styled-by-prettify"><br></span><span style=3D"color:=
#800" class=3D"m_2847652167001302833m_-1524075392614256550m_-54570566493752=
2652styled-by-prettify">#include</span><span style=3D"color:#000" class=3D"=
m_2847652167001302833m_-1524075392614256550m_-545705664937522652styled-by-p=
rettify"> </span><span style=3D"color:#080" class=3D"m_2847652167001302833m=
_-1524075392614256550m_-545705664937522652styled-by-prettify"><limits>=
;</span><span style=3D"color:#000" class=3D"m_2847652167001302833m_-1524075=
392614256550m_-545705664937522652styled-by-prettify"><br></span><span style=
=3D"color:#800" class=3D"m_2847652167001302833m_-1524075392614256550m_-5457=
05664937522652styled-by-prettify">#include</span><span style=3D"color:#000"=
class=3D"m_2847652167001302833m_-1524075392614256550m_-545705664937522652s=
tyled-by-prettify"> </span><span style=3D"color:#080" class=3D"m_2847652167=
001302833m_-1524075392614256550m_-545705664937522652styled-by-prettify"><=
;cmath></span><span style=3D"color:#000" class=3D"m_2847652167001302833m=
_-1524075392614256550m_-545705664937522652styled-by-prettify"><br><br></spa=
n><span style=3D"color:#008" class=3D"m_2847652167001302833m_-1524075392614=
256550m_-545705664937522652styled-by-prettify">template</span><span style=
=3D"color:#660" class=3D"m_2847652167001302833m_-1524075392614256550m_-5457=
05664937522652styled-by-prettify"><</span><span style=3D"color:#008" cla=
ss=3D"m_2847652167001302833m_-1524075392614256550m_-545705664937522652style=
d-by-prettify">typename</span><span style=3D"color:#000" class=3D"m_2847652=
167001302833m_-1524075392614256550m_-545705664937522652styled-by-prettify">=
T</span><span style=3D"color:#660" class=3D"m_2847652167001302833m_-152407=
5392614256550m_-545705664937522652styled-by-prettify">></span><span styl=
e=3D"color:#000" class=3D"m_2847652167001302833m_-1524075392614256550m_-545=
705664937522652styled-by-prettify"><br></span><span style=3D"color:#008" cl=
ass=3D"m_2847652167001302833m_-1524075392614256550m_-545705664937522652styl=
ed-by-prettify">concept</span><span style=3D"color:#000" class=3D"m_2847652=
167001302833m_-1524075392614256550m_-545705664937522652styled-by-prettify">=
</span><span style=3D"color:#008" class=3D"m_2847652167001302833m_-1524075=
392614256550m_-545705664937522652styled-by-prettify">bool</span><span style=
=3D"color:#000" class=3D"m_2847652167001302833m_-1524075392614256550m_-5457=
05664937522652styled-by-prettify"> </span><span style=3D"color:#606" class=
=3D"m_2847652167001302833m_-1524075392614256550m_-545705664937522652styled-=
by-prettify">Integral</span><span style=3D"color:#000" class=3D"m_284765216=
7001302833m_-1524075392614256550m_-545705664937522652styled-by-prettify"> <=
/span><span style=3D"color:#660" class=3D"m_2847652167001302833m_-152407539=
2614256550m_-545705664937522652styled-by-prettify">=3D</span><span style=3D=
"color:#000" class=3D"m_2847652167001302833m_-1524075392614256550m_-5457056=
64937522652styled-by-prettify"> std</span><span style=3D"color:#660" class=
=3D"m_2847652167001302833m_-1524075392614256550m_-545705664937522652styled-=
by-prettify">::</span><span style=3D"color:#000" class=3D"m_284765216700130=
2833m_-1524075392614256550m_-545705664937522652styled-by-prettify">is_integ=
ral</span><span style=3D"color:#660" class=3D"m_2847652167001302833m_-15240=
75392614256550m_-545705664937522652styled-by-prettify"><</span><span sty=
le=3D"color:#000" class=3D"m_2847652167001302833m_-1524075392614256550m_-54=
5705664937522652styled-by-prettify">T</span><span style=3D"color:#660" clas=
s=3D"m_2847652167001302833m_-1524075392614256550m_-545705664937522652styled=
-by-prettify">>::</span><span style=3D"color:#000" class=3D"m_2847652167=
001302833m_-1524075392614256550m_-545705664937522652styled-by-prettify">val=
ue</span><span style=3D"color:#660" class=3D"m_2847652167001302833m_-152407=
5392614256550m_-545705664937522652styled-by-prettify">;</span><span style=
=3D"color:#000" class=3D"m_2847652167001302833m_-1524075392614256550m_-5457=
05664937522652styled-by-prettify"><br><br></span><span style=3D"color:#008"=
class=3D"m_2847652167001302833m_-1524075392614256550m_-545705664937522652s=
tyled-by-prettify">auto</span><span style=3D"color:#000" class=3D"m_2847652=
167001302833m_-1524075392614256550m_-545705664937522652styled-by-prettify">=
uabs</span><span style=3D"color:#660" class=3D"m_2847652167001302833m_-152=
4075392614256550m_-545705664937522652styled-by-prettify">(</span><span styl=
e=3D"color:#008" class=3D"m_2847652167001302833m_-1524075392614256550m_-545=
705664937522652styled-by-prettify">const</span><span style=3D"color:#000" c=
lass=3D"m_2847652167001302833m_-1524075392614256550m_-545705664937522652sty=
led-by-prettify"> </span><span style=3D"color:#606" class=3D"m_284765216700=
1302833m_-1524075392614256550m_-545705664937522652styled-by-prettify">Integ=
ral</span><span style=3D"color:#000" class=3D"m_2847652167001302833m_-15240=
75392614256550m_-545705664937522652styled-by-prettify"> s</span><span style=
=3D"color:#660" class=3D"m_2847652167001302833m_-1524075392614256550m_-5457=
05664937522652styled-by-prettify">)</span><span style=3D"color:#000" class=
=3D"m_2847652167001302833m_-1524075392614256550m_-545705664937522652styled-=
by-prettify"><br></span><span style=3D"color:#660" class=3D"m_2847652167001=
302833m_-1524075392614256550m_-545705664937522652styled-by-prettify">{</spa=
n><span style=3D"color:#000" class=3D"m_2847652167001302833m_-1524075392614=
256550m_-545705664937522652styled-by-prettify"><br>=C2=A0 =C2=A0 </span><sp=
an style=3D"color:#008" class=3D"m_2847652167001302833m_-152407539261425655=
0m_-545705664937522652styled-by-prettify">using</span><span style=3D"color:=
#000" class=3D"m_2847652167001302833m_-1524075392614256550m_-54570566493752=
2652styled-by-prettify"> </span><span style=3D"color:#606" class=3D"m_28476=
52167001302833m_-1524075392614256550m_-545705664937522652styled-by-prettify=
">UIntegral_t</span><span style=3D"color:#000" class=3D"m_28476521670013028=
33m_-1524075392614256550m_-545705664937522652styled-by-prettify"> </span><s=
pan style=3D"color:#660" class=3D"m_2847652167001302833m_-15240753926142565=
50m_-545705664937522652styled-by-prettify">=3D</span><span style=3D"color:#=
000" class=3D"m_2847652167001302833m_-1524075392614256550m_-545705664937522=
652styled-by-prettify"> std</span><span style=3D"color:#660" class=3D"m_284=
7652167001302833m_-1524075392614256550m_-545705664937522652styled-by-pretti=
fy">::</span><span style=3D"color:#000" class=3D"m_2847652167001302833m_-15=
24075392614256550m_-545705664937522652styled-by-prettify">make_unsigned_t</=
span><span style=3D"color:#660" class=3D"m_2847652167001302833m_-1524075392=
614256550m_-545705664937522652styled-by-prettify"><</span><span style=3D=
"color:#000" class=3D"m_2847652167001302833m_-1524075392614256550m_-5457056=
64937522652styled-by-prettify">std</span><span style=3D"color:#660" class=
=3D"m_2847652167001302833m_-1524075392614256550m_-545705664937522652styled-=
by-prettify">::</span><span style=3D"color:#000" class=3D"m_284765216700130=
2833m_-1524075392614256550m_-545705664937522652styled-by-prettify">decay_t<=
/span><span style=3D"color:#660" class=3D"m_2847652167001302833m_-152407539=
2614256550m_-545705664937522652styled-by-prettify"><</span><span style=
=3D"color:#008" class=3D"m_2847652167001302833m_-1524075392614256550m_-5457=
05664937522652styled-by-prettify">decltype</span><span style=3D"color:#660"=
class=3D"m_2847652167001302833m_-1524075392614256550m_-545705664937522652s=
tyled-by-prettify">(</span><span style=3D"color:#000" class=3D"m_2847652167=
001302833m_-1524075392614256550m_-545705664937522652styled-by-prettify">s</=
span><span style=3D"color:#660" class=3D"m_2847652167001302833m_-1524075392=
614256550m_-545705664937522652styled-by-prettify">)>>;</span><span st=
yle=3D"color:#000" class=3D"m_2847652167001302833m_-1524075392614256550m_-5=
45705664937522652styled-by-prettify"><br>=C2=A0 =C2=A0 </span><span style=
=3D"color:#008" class=3D"m_2847652167001302833m_-1524075392614256550m_-5457=
05664937522652styled-by-prettify">if</span><span style=3D"color:#660" class=
=3D"m_2847652167001302833m_-1524075392614256550m_-545705664937522652styled-=
by-prettify">(</span><span style=3D"color:#000" class=3D"m_2847652167001302=
833m_-1524075392614256550m_-545705664937522652styled-by-prettify"> s </span=
><span style=3D"color:#660" class=3D"m_2847652167001302833m_-15240753926142=
56550m_-545705664937522652styled-by-prettify">>=3D</span><span style=3D"=
color:#000" class=3D"m_2847652167001302833m_-1524075392614256550m_-54570566=
4937522652styled-by-prettify"> </span><span style=3D"color:#066" class=3D"m=
_2847652167001302833m_-1524075392614256550m_-545705664937522652styled-by-pr=
ettify">0</span><span style=3D"color:#000" class=3D"m_2847652167001302833m_=
-1524075392614256550m_-545705664937522652styled-by-prettify"> </span><span =
style=3D"color:#660" class=3D"m_2847652167001302833m_-1524075392614256550m_=
-545705664937522652styled-by-prettify">)</span><span style=3D"color:#000" c=
lass=3D"m_2847652167001302833m_-1524075392614256550m_-545705664937522652sty=
led-by-prettify"><br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 </span><span style=3D"colo=
r:#008" class=3D"m_2847652167001302833m_-1524075392614256550m_-545705664937=
522652styled-by-prettify">return</span><span style=3D"color:#000" class=3D"=
m_2847652167001302833m_-1524075392614256550m_-545705664937522652styled-by-p=
rettify"> </span><span style=3D"color:#008" class=3D"m_2847652167001302833m=
_-1524075392614256550m_-545705664937522652styled-by-prettify">static_cast</=
span><span style=3D"color:#660" class=3D"m_2847652167001302833m_-1524075392=
614256550m_-545705664937522652styled-by-prettify"><</span><span style=3D=
"color:#606" class=3D"m_2847652167001302833m_-1524075392614256550m_-5457056=
64937522652styled-by-prettify">UIntegral_t</span><span style=3D"color:#660"=
class=3D"m_2847652167001302833m_-1524075392614256550m_-545705664937522652s=
tyled-by-prettify">>(</span><span style=3D"color:#000" class=3D"m_284765=
2167001302833m_-1524075392614256550m_-545705664937522652styled-by-prettify"=
>s</span><span style=3D"color:#660" class=3D"m_2847652167001302833m_-152407=
5392614256550m_-545705664937522652styled-by-prettify">);</span><span style=
=3D"color:#000" class=3D"m_2847652167001302833m_-1524075392614256550m_-5457=
05664937522652styled-by-prettify"><br>=C2=A0 =C2=A0 </span><span style=3D"c=
olor:#008" class=3D"m_2847652167001302833m_-1524075392614256550m_-545705664=
937522652styled-by-prettify">else</span><span style=3D"color:#000" class=3D=
"m_2847652167001302833m_-1524075392614256550m_-545705664937522652styled-by-=
prettify"><br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 </span><span style=3D"color:#008"=
class=3D"m_2847652167001302833m_-1524075392614256550m_-545705664937522652s=
tyled-by-prettify">return</span><span style=3D"color:#000" class=3D"m_28476=
52167001302833m_-1524075392614256550m_-545705664937522652styled-by-prettify=
"> </span><span style=3D"color:#008" class=3D"m_2847652167001302833m_-15240=
75392614256550m_-545705664937522652styled-by-prettify">static_cast</span><s=
pan style=3D"color:#660" class=3D"m_2847652167001302833m_-15240753926142565=
50m_-545705664937522652styled-by-prettify"><</span><span style=3D"color:=
#606" class=3D"m_2847652167001302833m_-1524075392614256550m_-54570566493752=
2652styled-by-prettify">UIntegral_t</span><span style=3D"color:#660" class=
=3D"m_2847652167001302833m_-1524075392614256550m_-545705664937522652styled-=
by-prettify">>(~</span><span style=3D"color:#000" class=3D"m_28476521670=
01302833m_-1524075392614256550m_-545705664937522652styled-by-prettify">s </=
span><span style=3D"color:#660" class=3D"m_2847652167001302833m_-1524075392=
614256550m_-545705664937522652styled-by-prettify">+</span><span style=3D"co=
lor:#000" class=3D"m_2847652167001302833m_-1524075392614256550m_-5457056649=
37522652styled-by-prettify"> </span><span style=3D"color:#066" class=3D"m_2=
847652167001302833m_-1524075392614256550m_-545705664937522652styled-by-pret=
tify">1</span><span style=3D"color:#660" class=3D"m_2847652167001302833m_-1=
524075392614256550m_-545705664937522652styled-by-prettify">);</span><span s=
tyle=3D"color:#000" class=3D"m_2847652167001302833m_-1524075392614256550m_-=
545705664937522652styled-by-prettify"><br></span><span style=3D"color:#660"=
class=3D"m_2847652167001302833m_-1524075392614256550m_-545705664937522652s=
tyled-by-prettify">}</span><span style=3D"color:#000" class=3D"m_2847652167=
001302833m_-1524075392614256550m_-545705664937522652styled-by-prettify"><br=
><br></span><span style=3D"color:#008" class=3D"m_2847652167001302833m_-152=
4075392614256550m_-545705664937522652styled-by-prettify">int</span><span st=
yle=3D"color:#000" class=3D"m_2847652167001302833m_-1524075392614256550m_-5=
45705664937522652styled-by-prettify"> main</span><span style=3D"color:#660"=
class=3D"m_2847652167001302833m_-1524075392614256550m_-545705664937522652s=
tyled-by-prettify">(</span><span style=3D"color:#008" class=3D"m_2847652167=
001302833m_-1524075392614256550m_-545705664937522652styled-by-prettify">voi=
d</span><span style=3D"color:#660" class=3D"m_2847652167001302833m_-1524075=
392614256550m_-545705664937522652styled-by-prettify">)</span><span style=3D=
"color:#000" class=3D"m_2847652167001302833m_-1524075392614256550m_-5457056=
64937522652styled-by-prettify"><br></span><span style=3D"color:#660" class=
=3D"m_2847652167001302833m_-1524075392614256550m_-545705664937522652styled-=
by-prettify">{</span><span style=3D"color:#000" class=3D"m_2847652167001302=
833m_-1524075392614256550m_-545705664937522652styled-by-prettify"><br>=C2=
=A0 =C2=A0 </span><span style=3D"color:#008" class=3D"m_2847652167001302833=
m_-1524075392614256550m_-545705664937522652styled-by-prettify">auto</span><=
span style=3D"color:#000" class=3D"m_2847652167001302833m_-1524075392614256=
550m_-545705664937522652styled-by-prettify"> i </span><span style=3D"color:=
#660" class=3D"m_2847652167001302833m_-1524075392614256550m_-54570566493752=
2652styled-by-prettify">=3D</span><span style=3D"color:#000" class=3D"m_284=
7652167001302833m_-1524075392614256550m_-545705664937522652styled-by-pretti=
fy"> </span><span style=3D"color:#008" class=3D"m_2847652167001302833m_-152=
4075392614256550m_-545705664937522652styled-by-prettify">static_cast</span>=
<span style=3D"color:#660" class=3D"m_2847652167001302833m_-152407539261425=
6550m_-545705664937522652styled-by-prettify"><</span><span style=3D"colo=
r:#000" class=3D"m_2847652167001302833m_-1524075392614256550m_-545705664937=
522652styled-by-prettify">std</span><span style=3D"color:#660" class=3D"m_2=
847652167001302833m_-1524075392614256550m_-545705664937522652styled-by-pret=
tify">::</span><span style=3D"color:#000" class=3D"m_2847652167001302833m_-=
1524075392614256550m_-545705664937522652styled-by-prettify">uint64_t</span>=
<span style=3D"color:#660" class=3D"m_2847652167001302833m_-152407539261425=
6550m_-545705664937522652styled-by-prettify">>(</span><span style=3D"col=
or:#000" class=3D"m_2847652167001302833m_-1524075392614256550m_-54570566493=
7522652styled-by-prettify">std</span><span style=3D"color:#660" class=3D"m_=
2847652167001302833m_-1524075392614256550m_-545705664937522652styled-by-pre=
ttify">::</span><span style=3D"color:#000" class=3D"m_2847652167001302833m_=
-1524075392614256550m_-545705664937522652styled-by-prettify">abs</span><spa=
n style=3D"color:#660" class=3D"m_2847652167001302833m_-1524075392614256550=
m_-545705664937522652styled-by-prettify">(</span><span style=3D"color:#066"=
class=3D"m_2847652167001302833m_-1524075392614256550m_-545705664937522652s=
tyled-by-prettify">1.0</span><span style=3D"color:#000" class=3D"m_28476521=
67001302833m_-1524075392614256550m_-545705664937522652styled-by-prettify"> =
</span><span style=3D"color:#660" class=3D"m_2847652167001302833m_-15240753=
92614256550m_-545705664937522652styled-by-prettify">*</span><span style=3D"=
color:#000" class=3D"m_2847652167001302833m_-1524075392614256550m_-54570566=
4937522652styled-by-prettify"> std</span><span style=3D"color:#660" class=
=3D"m_2847652167001302833m_-1524075392614256550m_-545705664937522652styled-=
by-prettify">::</span><span style=3D"color:#000" class=3D"m_284765216700130=
2833m_-1524075392614256550m_-545705664937522652styled-by-prettify">numeric_=
limits</span><span style=3D"color:#660" class=3D"m_2847652167001302833m_-15=
24075392614256550m_-545705664937522652styled-by-prettify"><</span><span =
style=3D"color:#000" class=3D"m_2847652167001302833m_-1524075392614256550m_=
-545705664937522652styled-by-prettify">std</span><span style=3D"color:#660"=
class=3D"m_2847652167001302833m_-1524075392614256550m_-545705664937522652s=
tyled-by-prettify">::</span><span style=3D"color:#000" class=3D"m_284765216=
7001302833m_-1524075392614256550m_-545705664937522652styled-by-prettify">in=
t64_t</span><span style=3D"color:#660" class=3D"m_2847652167001302833m_-152=
4075392614256550m_-545705664937522652styled-by-prettify">>::</span><span=
style=3D"color:#000" class=3D"m_2847652167001302833m_-1524075392614256550m=
_-545705664937522652styled-by-prettify">min</span><span style=3D"color:#660=
" class=3D"m_2847652167001302833m_-1524075392614256550m_-545705664937522652=
styled-by-prettify">()));</span><span style=3D"color:#000" class=3D"m_28476=
52167001302833m_-1524075392614256550m_-545705664937522652styled-by-prettify=
"> </span><span style=3D"color:#800" class=3D"m_2847652167001302833m_-15240=
75392614256550m_-545705664937522652styled-by-prettify">// Is not an option =
because of precision errors.</span><span style=3D"color:#000" class=3D"m_28=
47652167001302833m_-1524075392614256550m_-545705664937522652styled-by-prett=
ify"><br>=C2=A0 =C2=A0 </span><span style=3D"color:#008" class=3D"m_2847652=
167001302833m_-1524075392614256550m_-545705664937522652styled-by-prettify">=
return</span><span style=3D"color:#000" class=3D"m_2847652167001302833m_-15=
24075392614256550m_-545705664937522652styled-by-prettify"> uabs</span><span=
style=3D"color:#660" class=3D"m_2847652167001302833m_-1524075392614256550m=
_-545705664937522652styled-by-prettify">(</span><span style=3D"color:#000" =
class=3D"m_2847652167001302833m_-1524075392614256550m_-545705664937522652st=
yled-by-prettify">std</span><span style=3D"color:#660" class=3D"m_284765216=
7001302833m_-1524075392614256550m_-545705664937522652styled-by-prettify">::=
</span><span style=3D"color:#000" class=3D"m_2847652167001302833m_-15240753=
92614256550m_-545705664937522652styled-by-prettify">numeric_limits</span><s=
pan style=3D"color:#660" class=3D"m_2847652167001302833m_-15240753926142565=
50m_-545705664937522652styled-by-prettify"><</span><span style=3D"color:=
#000" class=3D"m_2847652167001302833m_-1524075392614256550m_-54570566493752=
2652styled-by-prettify">std</span><span style=3D"color:#660" class=3D"m_284=
7652167001302833m_-1524075392614256550m_-545705664937522652styled-by-pretti=
fy">::</span><span style=3D"color:#000" class=3D"m_2847652167001302833m_-15=
24075392614256550m_-545705664937522652styled-by-prettify">int8_t</span><spa=
n style=3D"color:#660" class=3D"m_2847652167001302833m_-1524075392614256550=
m_-545705664937522652styled-by-prettify">>::</span><span style=3D"color:=
#000" class=3D"m_2847652167001302833m_-1524075392614256550m_-54570566493752=
2652styled-by-prettify">min</span><span style=3D"color:#660" class=3D"m_284=
7652167001302833m_-1524075392614256550m_-545705664937522652styled-by-pretti=
fy">());</span><span style=3D"color:#000" class=3D"m_2847652167001302833m_-=
1524075392614256550m_-545705664937522652styled-by-prettify"><br></span><spa=
n style=3D"color:#660" class=3D"m_2847652167001302833m_-1524075392614256550=
m_-545705664937522652styled-by-prettify">}</span></div></code></div></div><=
/div><div><br></div><div>We could call it std::uabs or std::make_positive.<=
br>If there exists already something like this in the standard, please feel=
free to tell me!</div><div>And if not maybe you can come up with a better =
name.</div></div>
<p></p>
-- <br>
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" group.<br>
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org" target=3D"_=
blank">std-proposals+unsubscribe@isocpp.org</a>.<br>
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org" target=3D"_blank">std-proposals@isocpp.org</a>.<br>
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/3f8200be-918a-44a0-a412-6f8f43da1c51%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter" target=3D"_blank">=
https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/3f8200be-918a-=
44a0-a412-6f8f43da1c51%40isocpp.org</a>.<br>
</blockquote></div>
</div></div>
<p></p>
-- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/CABPJVnS%2BUYT9Q-6X3Y_t043%3D%3DND38c=
yA5048c1HjbVkX8q3cEw%40mail.gmail.com?utm_medium=3Demail&utm_source=3Dfoote=
r">https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CABPJVnS%2B=
UYT9Q-6X3Y_t043%3D%3DND38cyA5048c1HjbVkX8q3cEw%40mail.gmail.com</a>.<br />
--0000000000004b97fe05780f74cc--
.
Author: Bengt Gustafsson <bengt.gustafsson@beamways.com>
Date: Sat, 13 Oct 2018 19:31:16 -0700 (PDT)
Raw View
------=_Part_1070_234037989.1539484276879
Content-Type: multipart/alternative;
boundary="----=_Part_1071_77927388.1539484276879"
------=_Part_1071_77927388.1539484276879
Content-Type: text/plain; charset="UTF-8"
For me the motivation for uabs() would more be to avoid unsigned/signed
mismatch errors by getting the result as an unsigned (which we know it
conceptually is after abs).
This is very similar to the annoyance of floor, round and ceil returning
floats which we know have integer values, forcing us to write int(round(x))
when using this with other ints to avoid compiler warnings. I think I saw
something about new floor<int>() and similar functions being proposed, so
why not uabs?
--
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/3a5821ae-c386-46b4-8e73-4bff8764679c%40isocpp.org.
------=_Part_1071_77927388.1539484276879
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">For me the motivation for uabs() would more be to avoid un=
signed/signed mismatch errors by getting the result as an unsigned (which w=
e know it conceptually is after abs).<div><br></div><div>This is very simil=
ar to the annoyance of floor, round and ceil returning floats which we know=
have integer values, forcing us to write int(round(x)) when using this wit=
h other ints to avoid compiler warnings. I think I saw something about new =
floor<int>() and similar functions being proposed, so why not uabs?</=
div><div><br></div></div>
<p></p>
-- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/3a5821ae-c386-46b4-8e73-4bff8764679c%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/3a5821ae-c386-46b4-8e73-4bff8764679c=
%40isocpp.org</a>.<br />
------=_Part_1071_77927388.1539484276879--
------=_Part_1070_234037989.1539484276879--
.
Author: John McFarlane <john@mcfarlane.name>
Date: Sun, 14 Oct 2018 16:17:53 +0100
Raw View
--000000000000d2f2be057831d326
Content-Type: text/plain; charset="UTF-8"
On Sun, Oct 14, 2018, 03:31 Bengt Gustafsson <bengt.gustafsson@beamways.com>
wrote:
> For me the motivation for uabs() would more be to avoid unsigned/signed
> mismatch errors by getting the result as an unsigned (which we know it
> conceptually is after abs).
>
I consider a number being positive as different to a number being unsigned.
I'd rather those two things (conversion and absolute value) remained
separate concerns.
>
> This is very similar to the annoyance of floor, round and ceil returning
> floats which we know have integer values, forcing us to write int(round(x))
> when using this with other ints to avoid compiler warnings. I think I saw
> something about new floor<int>() and similar functions being proposed, so
> why not uabs?
>
I'm less adverse to abs<unsigned>() because the two concerns are more
apparent but only slightly.
If you're converting a float to an int, a necessary detail is how to
approximate fractional values. So a convert<int, floor_tag>() starts to
become palatable. p0105 proposed something similar. I'm not familiar with
the floor function you mention.
>
> --
> You received this message because you are subscribed to the Google Groups
> "ISO C++ Standard - Future Proposals" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to std-proposals+unsubscribe@isocpp.org.
> To post to this group, send email to std-proposals@isocpp.org.
> To view this discussion on the web visit
> https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/3a5821ae-c386-46b4-8e73-4bff8764679c%40isocpp.org
> <https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/3a5821ae-c386-46b4-8e73-4bff8764679c%40isocpp.org?utm_medium=email&utm_source=footer>
> .
>
--
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CABPJVnTNih2bKvp6rVCWBpnv06KYuXDs3M0HwZSU_JPV3oG%3D8g%40mail.gmail.com.
--000000000000d2f2be057831d326
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
<br><br><div class=3D"gmail_quote"><div dir=3D"ltr">On Sun, Oct 14, 2018, 0=
3:31 Bengt Gustafsson <<a href=3D"mailto:bengt.gustafsson@beamways.com">=
bengt.gustafsson@beamways.com</a>> wrote:<br></div><blockquote class=3D"=
gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-=
left:1ex"><div dir=3D"ltr">For me the motivation for uabs() would more be t=
o avoid unsigned/signed mismatch errors by getting the result as an unsigne=
d (which we know it conceptually is after abs).</div></blockquote></div><di=
v><br></div><div>I consider a number being positive as different to a numbe=
r being unsigned. I'd rather those two things (conversion and absolute =
value)=C2=A0remained separate concerns.<br></div><div class=3D"gmail_quote"=
><blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1=
px #ccc solid;padding-left:1ex"><div dir=3D"ltr"><div><br></div><div>This i=
s very similar to the annoyance of floor, round and ceil returning floats w=
hich we know have integer values, forcing us to write int(round(x)) when us=
ing this with other ints to avoid compiler warnings. I think I saw somethin=
g about new floor<int>() and similar functions being proposed, so why=
not uabs?</div></div></blockquote></div><div><br></div><div>I'm less a=
dverse to abs<unsigned>() because the two concerns are more apparent =
but only slightly.=C2=A0</div><div><br></div><div>If you're converting =
a float to an int, a necessary detail is how to approximate fractional valu=
es. So a convert<int, floor_tag>() starts to become palatable. p0105 =
proposed something similar. I'm not familiar with the floor function yo=
u mention.</div><div class=3D"gmail_quote"><blockquote class=3D"gmail_quote=
" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><=
div dir=3D"ltr"><div><br></div></div>
<p></p>
-- <br>
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" group.<br>
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org" target=3D"_=
blank">std-proposals+unsubscribe@isocpp.org</a>.<br>
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org" target=3D"_blank">std-proposals@isocpp.org</a>.<br>
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/3a5821ae-c386-46b4-8e73-4bff8764679c%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter" target=3D"_blank">=
https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/3a5821ae-c386-=
46b4-8e73-4bff8764679c%40isocpp.org</a>.<br>
</blockquote></div>
<p></p>
-- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/CABPJVnTNih2bKvp6rVCWBpnv06KYuXDs3M0H=
wZSU_JPV3oG%3D8g%40mail.gmail.com?utm_medium=3Demail&utm_source=3Dfooter">h=
ttps://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CABPJVnTNih2bKv=
p6rVCWBpnv06KYuXDs3M0HwZSU_JPV3oG%3D8g%40mail.gmail.com</a>.<br />
--000000000000d2f2be057831d326--
.
Author: burningorca@gmail.com
Date: Sat, 20 Oct 2018 04:25:19 -0700 (PDT)
Raw View
------=_Part_58_848282328.1540034719166
Content-Type: multipart/alternative;
boundary="----=_Part_59_483986967.1540034719167"
------=_Part_59_483986967.1540034719167
Content-Type: text/plain; charset="UTF-8"
But the result of round, ceil and float may not result in a value that is
able to be stored in an integer. The result of abs can always be stored in
unsigned same type or any greater type.
abs, floor, ceil and round are all C-function which do not have the power
of templates.
But if we would have something like:
#include <cstdint>
#include <limits>
#include <iostream>
#include <type_traits>
template<typename T>
concept bool Integral = std::is_integral<T>::value;
template<Integral ResultType, Integral ValueType>
auto abs(const ValueType s) -> ResultType
{
static_assert(sizeof(ResultType) > sizeof(decltype(s)) || std::is_same_v
<ResultType, std::make_unsigned_t<ValueType>>, "The result type must be
able to store the positive value!");
ResultType absVal;
if( s >= 0 )
{
return static_cast<ResultType>(s);
}
else
{
absVal = ~s;
absVal++;
return absVal;
}
}
int main(void)
{
std::cout << abs<std::uint8_t>(std::numeric_limits<std::int8_t>::min())
<< '\n';
std::cout << abs<std::int32_t>(std::numeric_limits<std::int16_t>::min())
<< '\n';
//std::cout <<
abs<std::uint8_t>(std::numeric_limits<std::int64_t>::min()) << '\n';
return 0;
}
it can be named abs, because as the result type must be specified it is
different from the existing abs overloads and thus does not need a new name.
So I agree to abs<unsigned>();
And if you do not care about safety you could even leave out the
static_assert. We could also make the return type be any arithmetic type.
In this case floor<int>(), ceil<int>() and round<int>() and maybe other
similar functions could also be added.
Am Sonntag, 14. Oktober 2018 17:18:09 UTC+2 schrieb John McFarlane:
>
>
>
> On Sun, Oct 14, 2018, 03:31 Bengt Gustafsson <bengt.gu...@beamways.com
> <javascript:>> wrote:
>
>> For me the motivation for uabs() would more be to avoid unsigned/signed
>> mismatch errors by getting the result as an unsigned (which we know it
>> conceptually is after abs).
>>
>
> I consider a number being positive as different to a number being
> unsigned. I'd rather those two things (conversion and absolute
> value) remained separate concerns.
>
>>
>> This is very similar to the annoyance of floor, round and ceil returning
>> floats which we know have integer values, forcing us to write int(round(x))
>> when using this with other ints to avoid compiler warnings. I think I saw
>> something about new floor<int>() and similar functions being proposed, so
>> why not uabs?
>>
>
> I'm less adverse to abs<unsigned>() because the two concerns are more
> apparent but only slightly.
>
> If you're converting a float to an int, a necessary detail is how to
> approximate fractional values. So a convert<int, floor_tag>() starts to
> become palatable. p0105 proposed something similar. I'm not familiar with
> the floor function you mention.
>
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "ISO C++ Standard - Future Proposals" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to std-proposal...@isocpp.org <javascript:>.
>> To post to this group, send email to std-pr...@isocpp.org <javascript:>.
>> To view this discussion on the web visit
>> https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/3a5821ae-c386-46b4-8e73-4bff8764679c%40isocpp.org
>> <https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/3a5821ae-c386-46b4-8e73-4bff8764679c%40isocpp.org?utm_medium=email&utm_source=footer>
>> .
>>
>
--
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/956410ae-7e45-43a7-ae1d-52a92aec6217%40isocpp.org.
------=_Part_59_483986967.1540034719167
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr"><div>But the result of round, ceil and float may not resul=
t in a value that is able to be stored in an integer. The result of abs can=
always be stored in=C2=A0 unsigned same type or any greater type.</div><di=
v>abs, floor, ceil and round are all C-function which do not have the power=
of templates.</div><div><br></div><div>But if we would have something like=
:</div><div><br></div><div><div style=3D"background-color: rgb(250, 250, 25=
0); border-color: rgb(187, 187, 187); border-style: solid; border-width: 1p=
x; overflow-wrap: break-word;" class=3D"prettyprint"><code class=3D"prettyp=
rint"><div class=3D"subprettyprint"><span style=3D"color: #800;" class=3D"s=
tyled-by-prettify">#include</span><span style=3D"color: #000;" class=3D"sty=
led-by-prettify"> </span><span style=3D"color: #080;" class=3D"styled-by-pr=
ettify"><cstdint></span><span style=3D"color: #000;" class=3D"styled-=
by-prettify"><br></span><span style=3D"color: #800;" class=3D"styled-by-pre=
ttify">#include</span><span style=3D"color: #000;" class=3D"styled-by-prett=
ify"> </span><span style=3D"color: #080;" class=3D"styled-by-prettify"><=
limits></span><span style=3D"color: #000;" class=3D"styled-by-prettify">=
<br></span><span style=3D"color: #800;" class=3D"styled-by-prettify">#inclu=
de</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span>=
<span style=3D"color: #080;" class=3D"styled-by-prettify"><iostream><=
/span><span style=3D"color: #000;" class=3D"styled-by-prettify"><br></span>=
<span style=3D"color: #800;" class=3D"styled-by-prettify">#include</span><s=
pan style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=
=3D"color: #080;" class=3D"styled-by-prettify"><type_traits></span><s=
pan style=3D"color: #000;" class=3D"styled-by-prettify"><br><br></span><spa=
n style=3D"color: #008;" class=3D"styled-by-prettify">template</span><span =
style=3D"color: #660;" class=3D"styled-by-prettify"><</span><span style=
=3D"color: #008;" class=3D"styled-by-prettify">typename</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify"> T</span><span style=3D"colo=
r: #660;" class=3D"styled-by-prettify">></span><span style=3D"color: #00=
0;" class=3D"styled-by-prettify"><br></span><span style=3D"color: #008;" cl=
ass=3D"styled-by-prettify">concept</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify"> </span><span style=3D"color: #008;" class=3D"style=
d-by-prettify">bool</span><span style=3D"color: #000;" class=3D"styled-by-p=
rettify"> </span><span style=3D"color: #606;" class=3D"styled-by-prettify">=
Integral</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> <=
/span><span style=3D"color: #660;" class=3D"styled-by-prettify">=3D</span><=
span style=3D"color: #000;" class=3D"styled-by-prettify"> std</span><span s=
tyle=3D"color: #660;" class=3D"styled-by-prettify">::</span><span style=3D"=
color: #000;" class=3D"styled-by-prettify">is_integral</span><span style=3D=
"color: #660;" class=3D"styled-by-prettify"><</span><span style=3D"color=
: #000;" class=3D"styled-by-prettify">T</span><span style=3D"color: #660;" =
class=3D"styled-by-prettify">>::</span><span style=3D"color: #000;" clas=
s=3D"styled-by-prettify">value</span><span style=3D"color: #660;" class=3D"=
styled-by-prettify">;</span><span style=3D"color: #000;" class=3D"styled-by=
-prettify"><br><br></span><span style=3D"color: #008;" class=3D"styled-by-p=
rettify">template</span><span style=3D"color: #660;" class=3D"styled-by-pre=
ttify"><</span><span style=3D"color: #606;" class=3D"styled-by-prettify"=
>Integral</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> =
</span><span style=3D"color: #606;" class=3D"styled-by-prettify">ResultType=
</span><span style=3D"color: #660;" class=3D"styled-by-prettify">,</span><s=
pan style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=
=3D"color: #606;" class=3D"styled-by-prettify">Integral</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color=
: #606;" class=3D"styled-by-prettify">ValueType</span><span style=3D"color:=
#660;" class=3D"styled-by-prettify">></span><span style=3D"color: #000;=
" class=3D"styled-by-prettify"><br></span><span style=3D"color: #008;" clas=
s=3D"styled-by-prettify">auto</span><span style=3D"color: #000;" class=3D"s=
tyled-by-prettify"> abs</span><span style=3D"color: #660;" class=3D"styled-=
by-prettify">(</span><span style=3D"color: #008;" class=3D"styled-by-pretti=
fy">const</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> =
</span><span style=3D"color: #606;" class=3D"styled-by-prettify">ValueType<=
/span><span style=3D"color: #000;" class=3D"styled-by-prettify"> s</span><s=
pan style=3D"color: #660;" class=3D"styled-by-prettify">)</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color=
: #660;" class=3D"styled-by-prettify">-></span><span style=3D"color: #00=
0;" class=3D"styled-by-prettify"> </span><span style=3D"color: #606;" class=
=3D"styled-by-prettify">ResultType</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify"><br></span><span style=3D"color: #660;" class=3D"st=
yled-by-prettify">{</span><span style=3D"color: #000;" class=3D"styled-by-p=
rettify"><br>=C2=A0 =C2=A0 </span><span style=3D"color: #008;" class=3D"sty=
led-by-prettify">static_assert</span><span style=3D"color: #660;" class=3D"=
styled-by-prettify">(</span><span style=3D"color: #008;" class=3D"styled-by=
-prettify">sizeof</span><span style=3D"color: #660;" class=3D"styled-by-pre=
ttify">(</span><span style=3D"color: #606;" class=3D"styled-by-prettify">Re=
sultType</span><span style=3D"color: #660;" class=3D"styled-by-prettify">)<=
/span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><sp=
an style=3D"color: #660;" class=3D"styled-by-prettify">></span><span sty=
le=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"col=
or: #008;" class=3D"styled-by-prettify">sizeof</span><span style=3D"color: =
#660;" class=3D"styled-by-prettify">(</span><span style=3D"color: #008;" cl=
ass=3D"styled-by-prettify">decltype</span><span style=3D"color: #660;" clas=
s=3D"styled-by-prettify">(</span><span style=3D"color: #000;" class=3D"styl=
ed-by-prettify">s</span><span style=3D"color: #660;" class=3D"styled-by-pre=
ttify">))</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> =
</span><span style=3D"color: #660;" class=3D"styled-by-prettify">||</span><=
span style=3D"color: #000;" class=3D"styled-by-prettify"> std</span><span s=
tyle=3D"color: #660;" class=3D"styled-by-prettify">::</span><span style=3D"=
color: #000;" class=3D"styled-by-prettify">is_same_v</span><span style=3D"c=
olor: #660;" class=3D"styled-by-prettify"><</span><span style=3D"color: =
#606;" class=3D"styled-by-prettify">ResultType</span><span style=3D"color: =
#660;" class=3D"styled-by-prettify">,</span><span style=3D"color: #000;" cl=
ass=3D"styled-by-prettify"> std</span><span style=3D"color: #660;" class=3D=
"styled-by-prettify">::</span><span style=3D"color: #000;" class=3D"styled-=
by-prettify">make_unsigned_t</span><span style=3D"color: #660;" class=3D"st=
yled-by-prettify"><</span><span style=3D"color: #606;" class=3D"styled-b=
y-prettify">ValueType</span><span style=3D"color: #660;" class=3D"styled-by=
-prettify">>>,</span><span style=3D"color: #000;" class=3D"styled-by-=
prettify"> </span><span style=3D"color: #080;" class=3D"styled-by-prettify"=
>"The result type must be able to store the positive value!"</spa=
n><span style=3D"color: #660;" class=3D"styled-by-prettify">);</span><span =
style=3D"color: #000;" class=3D"styled-by-prettify"><br>=C2=A0 =C2=A0 </spa=
n><span style=3D"color: #606;" class=3D"styled-by-prettify">ResultType</spa=
n><span style=3D"color: #000;" class=3D"styled-by-prettify"> absVal</span><=
span style=3D"color: #660;" class=3D"styled-by-prettify">;</span><span styl=
e=3D"color: #000;" class=3D"styled-by-prettify"><br>=C2=A0 =C2=A0 <br>=C2=
=A0 =C2=A0 </span><span style=3D"color: #008;" class=3D"styled-by-prettify"=
>if</span><span style=3D"color: #660;" class=3D"styled-by-prettify">(</span=
><span style=3D"color: #000;" class=3D"styled-by-prettify"> s </span><span =
style=3D"color: #660;" class=3D"styled-by-prettify">>=3D</span><span sty=
le=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"col=
or: #066;" class=3D"styled-by-prettify">0</span><span style=3D"color: #000;=
" class=3D"styled-by-prettify"> </span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">)</span><span style=3D"color: #000;" class=3D"style=
d-by-prettify"><br>=C2=A0 =C2=A0 </span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">{</span><span style=3D"color: #000;" class=3D"style=
d-by-prettify"><br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 </span><span style=3D"color:=
#008;" class=3D"styled-by-prettify">return</span><span style=3D"color: #00=
0;" class=3D"styled-by-prettify"> </span><span style=3D"color: #008;" class=
=3D"styled-by-prettify">static_cast</span><span style=3D"color: #660;" clas=
s=3D"styled-by-prettify"><</span><span style=3D"color: #606;" class=3D"s=
tyled-by-prettify">ResultType</span><span style=3D"color: #660;" class=3D"s=
tyled-by-prettify">>(</span><span style=3D"color: #000;" class=3D"styled=
-by-prettify">s</span><span style=3D"color: #660;" class=3D"styled-by-prett=
ify">);</span><span style=3D"color: #000;" class=3D"styled-by-prettify"><br=
>=C2=A0 =C2=A0 </span><span style=3D"color: #660;" class=3D"styled-by-prett=
ify">}</span><span style=3D"color: #000;" class=3D"styled-by-prettify"><br>=
=C2=A0 =C2=A0 </span><span style=3D"color: #008;" class=3D"styled-by-pretti=
fy">else</span><span style=3D"color: #000;" class=3D"styled-by-prettify"><b=
r>=C2=A0 =C2=A0 </span><span style=3D"color: #660;" class=3D"styled-by-pret=
tify">{</span><span style=3D"color: #000;" class=3D"styled-by-prettify"><br=
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 absVal </span><span style=3D"color: #660;" cla=
ss=3D"styled-by-prettify">=3D</span><span style=3D"color: #000;" class=3D"s=
tyled-by-prettify"> </span><span style=3D"color: #660;" class=3D"styled-by-=
prettify">~</span><span style=3D"color: #000;" class=3D"styled-by-prettify"=
>s</span><span style=3D"color: #660;" class=3D"styled-by-prettify">;</span>=
<span style=3D"color: #000;" class=3D"styled-by-prettify"><br>=C2=A0 =C2=A0=
=C2=A0 =C2=A0 absVal</span><span style=3D"color: #660;" class=3D"styled-by=
-prettify">++;</span><span style=3D"color: #000;" class=3D"styled-by-pretti=
fy"><br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 </span><span style=3D"color: #008;" cla=
ss=3D"styled-by-prettify">return</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify"> absVal</span><span style=3D"color: #660;" class=3D=
"styled-by-prettify">;</span><span style=3D"color: #000;" class=3D"styled-b=
y-prettify"><br>=C2=A0 =C2=A0 </span><span style=3D"color: #660;" class=3D"=
styled-by-prettify">}</span><span style=3D"color: #000;" class=3D"styled-by=
-prettify"><br></span><span style=3D"color: #660;" class=3D"styled-by-prett=
ify">}</span><span style=3D"color: #000;" class=3D"styled-by-prettify"><br>=
<br></span><span style=3D"color: #008;" class=3D"styled-by-prettify">int</s=
pan><span style=3D"color: #000;" class=3D"styled-by-prettify"> main</span><=
span style=3D"color: #660;" class=3D"styled-by-prettify">(</span><span styl=
e=3D"color: #008;" class=3D"styled-by-prettify">void</span><span style=3D"c=
olor: #660;" class=3D"styled-by-prettify">)</span><span style=3D"color: #00=
0;" class=3D"styled-by-prettify"><br></span><span style=3D"color: #660;" cl=
ass=3D"styled-by-prettify">{</span><span style=3D"color: #000;" class=3D"st=
yled-by-prettify"><br>=C2=A0 =C2=A0 std</span><span style=3D"color: #660;" =
class=3D"styled-by-prettify">::</span><span style=3D"color: #000;" class=3D=
"styled-by-prettify">cout </span><span style=3D"color: #660;" class=3D"styl=
ed-by-prettify"><<</span><span style=3D"color: #000;" class=3D"styled=
-by-prettify"> abs</span><span style=3D"color: #660;" class=3D"styled-by-pr=
ettify"><</span><span style=3D"color: #000;" class=3D"styled-by-prettify=
">std</span><span style=3D"color: #660;" class=3D"styled-by-prettify">::</s=
pan><span style=3D"color: #000;" class=3D"styled-by-prettify">uint8_t</span=
><span style=3D"color: #660;" class=3D"styled-by-prettify">>(</span><spa=
n style=3D"color: #000;" class=3D"styled-by-prettify">std</span><span style=
=3D"color: #660;" class=3D"styled-by-prettify">::</span><span style=3D"colo=
r: #000;" class=3D"styled-by-prettify">numeric_limits</span><span style=3D"=
color: #660;" class=3D"styled-by-prettify"><</span><span style=3D"color:=
#000;" class=3D"styled-by-prettify">std</span><span style=3D"color: #660;"=
class=3D"styled-by-prettify">::</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify">int8_t</span><span style=3D"color: #660;" class=3D"=
styled-by-prettify">>::</span><span style=3D"color: #000;" class=3D"styl=
ed-by-prettify">min</span><span style=3D"color: #660;" class=3D"styled-by-p=
rettify">())</span><span style=3D"color: #000;" class=3D"styled-by-prettify=
"> </span><span style=3D"color: #660;" class=3D"styled-by-prettify"><<=
;</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><=
span style=3D"color: #080;" class=3D"styled-by-prettify">'\n'</span=
><span style=3D"color: #660;" class=3D"styled-by-prettify">;</span><span st=
yle=3D"color: #000;" class=3D"styled-by-prettify"><br>=C2=A0 =C2=A0 std</sp=
an><span style=3D"color: #660;" class=3D"styled-by-prettify">::</span><span=
style=3D"color: #000;" class=3D"styled-by-prettify">cout </span><span styl=
e=3D"color: #660;" class=3D"styled-by-prettify"><<</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify"> abs</span><span style=3D"co=
lor: #660;" class=3D"styled-by-prettify"><</span><span style=3D"color: #=
000;" class=3D"styled-by-prettify">std</span><span style=3D"color: #660;" c=
lass=3D"styled-by-prettify">::</span><span style=3D"color: #000;" class=3D"=
styled-by-prettify">int32_t</span><span style=3D"color: #660;" class=3D"sty=
led-by-prettify">>(</span><span style=3D"color: #000;" class=3D"styled-b=
y-prettify">std</span><span style=3D"color: #660;" class=3D"styled-by-prett=
ify">::</span><span style=3D"color: #000;" class=3D"styled-by-prettify">num=
eric_limits</span><span style=3D"color: #660;" class=3D"styled-by-prettify"=
><</span><span style=3D"color: #000;" class=3D"styled-by-prettify">std</=
span><span style=3D"color: #660;" class=3D"styled-by-prettify">::</span><sp=
an style=3D"color: #000;" class=3D"styled-by-prettify">int16_t</span><span =
style=3D"color: #660;" class=3D"styled-by-prettify">>::</span><span styl=
e=3D"color: #000;" class=3D"styled-by-prettify">min</span><span style=3D"co=
lor: #660;" class=3D"styled-by-prettify">())</span><span style=3D"color: #0=
00;" class=3D"styled-by-prettify"> </span><span style=3D"color: #660;" clas=
s=3D"styled-by-prettify"><<</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify"> </span><span style=3D"color: #080;" class=3D"style=
d-by-prettify">'\n'</span><span style=3D"color: #660;" class=3D"sty=
led-by-prettify">;</span><span style=3D"color: #000;" class=3D"styled-by-pr=
ettify"><br>=C2=A0 =C2=A0 </span><span style=3D"color: #800;" class=3D"styl=
ed-by-prettify">//std::cout << abs<std::uint8_t>(std::numeric_l=
imits<std::int64_t>::min()) << '\n';</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify"><br>=C2=A0 =C2=A0 </span><sp=
an style=3D"color: #008;" class=3D"styled-by-prettify">return</span><span s=
tyle=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"c=
olor: #066;" class=3D"styled-by-prettify">0</span><span style=3D"color: #66=
0;" class=3D"styled-by-prettify">;</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify"><br></span><span style=3D"color: #660;" class=3D"st=
yled-by-prettify">}</span></div></code></div><br>it can be named abs, becau=
se as the result type must be specified it is different from the existing a=
bs overloads and thus does not need a new name.</div><div>So I agree to abs=
<unsigned>();</div><div>And if you do not care about safety you could=
even leave out the static_assert. We could also make the return type be an=
y arithmetic type.<br></div><div>In this case floor<int>(), ceil<i=
nt>() and round<int>() and maybe other similar functions could als=
o be added.</div><br>Am Sonntag, 14. Oktober 2018 17:18:09 UTC+2 schrieb Jo=
hn McFarlane:<blockquote class=3D"gmail_quote" style=3D"margin: 0;margin-le=
ft: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;"><br><br><div clas=
s=3D"gmail_quote"><div dir=3D"ltr">On Sun, Oct 14, 2018, 03:31 Bengt Gustaf=
sson <<a href=3D"javascript:" target=3D"_blank" gdf-obfuscated-mailto=3D=
"M4IUXMoOBAAJ" rel=3D"nofollow" onmousedown=3D"this.href=3D'javascript:=
';return true;" onclick=3D"this.href=3D'javascript:';return tru=
e;">bengt.gu...@beamways.com</a><wbr>> wrote:<br></div><blockquote class=
=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padd=
ing-left:1ex"><div dir=3D"ltr">For me the motivation for uabs() would more =
be to avoid unsigned/signed mismatch errors by getting the result as an uns=
igned (which we know it conceptually is after abs).</div></blockquote></div=
><div><br></div><div>I consider a number being positive as different to a n=
umber being unsigned. I'd rather those two things (conversion and absol=
ute value)=C2=A0remained separate concerns.<br></div><div class=3D"gmail_qu=
ote"><blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-le=
ft:1px #ccc solid;padding-left:1ex"><div dir=3D"ltr"><div><br></div><div>Th=
is is very similar to the annoyance of floor, round and ceil returning floa=
ts which we know have integer values, forcing us to write int(round(x)) whe=
n using this with other ints to avoid compiler warnings. I think I saw some=
thing about new floor<int>() and similar functions being proposed, so=
why not uabs?</div></div></blockquote></div><div><br></div><div>I'm le=
ss adverse to abs<unsigned>() because the two concerns are more appar=
ent but only slightly.=C2=A0</div><div><br></div><div>If you're convert=
ing a float to an int, a necessary detail is how to approximate fractional =
values. So a convert<int, floor_tag>() starts to become palatable. p0=
105 proposed something similar. I'm not familiar with the floor functio=
n you mention.</div><div class=3D"gmail_quote"><blockquote class=3D"gmail_q=
uote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1e=
x"><div dir=3D"ltr"><div><br></div></div>
<p></p>
-- <br>
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" group.<br>
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"javascript:" target=3D"_blank" gdf-obfuscated-mailto=3D"=
M4IUXMoOBAAJ" rel=3D"nofollow" onmousedown=3D"this.href=3D'javascript:&=
#39;;return true;" onclick=3D"this.href=3D'javascript:';return true=
;">std-proposal...@<wbr>isocpp.org</a>.<br>
To post to this group, send email to <a href=3D"javascript:" target=3D"_bla=
nk" gdf-obfuscated-mailto=3D"M4IUXMoOBAAJ" rel=3D"nofollow" onmousedown=3D"=
this.href=3D'javascript:';return true;" onclick=3D"this.href=3D'=
;javascript:';return true;">std-pr...@isocpp.org</a>.<br>
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/3a5821ae-c386-46b4-8e73-4bff8764679c%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter" target=3D"_blank" =
rel=3D"nofollow" onmousedown=3D"this.href=3D'https://groups.google.com/=
a/isocpp.org/d/msgid/std-proposals/3a5821ae-c386-46b4-8e73-4bff8764679c%40i=
socpp.org?utm_medium\x3demail\x26utm_source\x3dfooter';return true;" on=
click=3D"this.href=3D'https://groups.google.com/a/isocpp.org/d/msgid/st=
d-proposals/3a5821ae-c386-46b4-8e73-4bff8764679c%40isocpp.org?utm_medium\x3=
demail\x26utm_source\x3dfooter';return true;">https://groups.google.com=
/a/<wbr>isocpp.org/d/msgid/std-<wbr>proposals/3a5821ae-c386-46b4-<wbr>8e73-=
4bff8764679c%40isocpp.org</a><wbr>.<br>
</blockquote></div>
</blockquote></div>
<p></p>
-- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/956410ae-7e45-43a7-ae1d-52a92aec6217%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/956410ae-7e45-43a7-ae1d-52a92aec6217=
%40isocpp.org</a>.<br />
------=_Part_59_483986967.1540034719167--
------=_Part_58_848282328.1540034719166--
.
Author: burningorca@gmail.com
Date: Sat, 20 Oct 2018 04:27:31 -0700 (PDT)
Raw View
------=_Part_4135_1304879855.1540034851853
Content-Type: multipart/alternative;
boundary="----=_Part_4136_1414481924.1540034851854"
------=_Part_4136_1414481924.1540034851854
Content-Type: text/plain; charset="UTF-8"
I recently read that their is a paper suggesting integers always being twos
complement should be standardised. If this is the case, would the call to
the existing std::abs(std::numeric_limits<SignedType>::min()) become well
defined behaviour?
Am Samstag, 20. Oktober 2018 13:25:19 UTC+2 schrieb burni...@gmail.com:
>
> But the result of round, ceil and float may not result in a value that is
> able to be stored in an integer. The result of abs can always be stored in
> unsigned same type or any greater type.
> abs, floor, ceil and round are all C-function which do not have the power
> of templates.
>
> But if we would have something like:
>
> #include <cstdint>
> #include <limits>
> #include <iostream>
> #include <type_traits>
>
> template<typename T>
> concept bool Integral = std::is_integral<T>::value;
>
> template<Integral ResultType, Integral ValueType>
> auto abs(const ValueType s) -> ResultType
> {
> static_assert(sizeof(ResultType) > sizeof(decltype(s)) || std::
> is_same_v<ResultType, std::make_unsigned_t<ValueType>>, "The result type
> must be able to store the positive value!");
> ResultType absVal;
>
> if( s >= 0 )
> {
> return static_cast<ResultType>(s);
> }
> else
> {
> absVal = ~s;
> absVal++;
> return absVal;
> }
> }
>
> int main(void)
> {
> std::cout << abs<std::uint8_t>(std::numeric_limits<std::int8_t>::min
> ()) << '\n';
> std::cout << abs<std::int32_t>(std::numeric_limits<std::int16_t>::min
> ()) << '\n';
> //std::cout <<
> abs<std::uint8_t>(std::numeric_limits<std::int64_t>::min()) << '\n';
> return 0;
> }
>
> it can be named abs, because as the result type must be specified it is
> different from the existing abs overloads and thus does not need a new name.
> So I agree to abs<unsigned>();
> And if you do not care about safety you could even leave out the
> static_assert. We could also make the return type be any arithmetic type.
> In this case floor<int>(), ceil<int>() and round<int>() and maybe other
> similar functions could also be added.
>
> Am Sonntag, 14. Oktober 2018 17:18:09 UTC+2 schrieb John McFarlane:
>>
>>
>>
>> On Sun, Oct 14, 2018, 03:31 Bengt Gustafsson <bengt.gu...@beamways.com>
>> wrote:
>>
>>> For me the motivation for uabs() would more be to avoid unsigned/signed
>>> mismatch errors by getting the result as an unsigned (which we know it
>>> conceptually is after abs).
>>>
>>
>> I consider a number being positive as different to a number being
>> unsigned. I'd rather those two things (conversion and absolute
>> value) remained separate concerns.
>>
>>>
>>> This is very similar to the annoyance of floor, round and ceil returning
>>> floats which we know have integer values, forcing us to write int(round(x))
>>> when using this with other ints to avoid compiler warnings. I think I saw
>>> something about new floor<int>() and similar functions being proposed, so
>>> why not uabs?
>>>
>>
>> I'm less adverse to abs<unsigned>() because the two concerns are more
>> apparent but only slightly.
>>
>> If you're converting a float to an int, a necessary detail is how to
>> approximate fractional values. So a convert<int, floor_tag>() starts to
>> become palatable. p0105 proposed something similar. I'm not familiar with
>> the floor function you mention.
>>
>>>
>>> --
>>> You received this message because you are subscribed to the Google
>>> Groups "ISO C++ Standard - Future Proposals" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to std-proposal...@isocpp.org.
>>> To post to this group, send email to std-pr...@isocpp.org.
>>> To view this discussion on the web visit
>>> https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/3a5821ae-c386-46b4-8e73-4bff8764679c%40isocpp.org
>>> <https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/3a5821ae-c386-46b4-8e73-4bff8764679c%40isocpp.org?utm_medium=email&utm_source=footer>
>>> .
>>>
>>
--
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/9d74ddbb-cc68-412a-ae6b-4664660ff12c%40isocpp.org.
------=_Part_4136_1414481924.1540034851854
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">I recently read that their is a paper suggesting integers =
always being twos complement should be standardised. If this is the case, w=
ould the call to the existing std::abs(std::numeric_limits<SignedType>=
;::min()) become well defined behaviour?<br><br>Am Samstag, 20. Oktober 201=
8 13:25:19 UTC+2 schrieb burni...@gmail.com:<blockquote class=3D"gmail_quot=
e" style=3D"margin: 0;margin-left: 0.8ex;border-left: 1px #ccc solid;paddin=
g-left: 1ex;"><div dir=3D"ltr"><div>But the result of round, ceil and float=
may not result in a value that is able to be stored in an integer. The res=
ult of abs can always be stored in=C2=A0 unsigned same type or any greater =
type.</div><div>abs, floor, ceil and round are all C-function which do not =
have the power of templates.</div><div><br></div><div>But if we would have =
something like:</div><div><br></div><div><div style=3D"background-color:rgb=
(250,250,250);border-color:rgb(187,187,187);border-style:solid;border-width=
:1px"><code><div><span style=3D"color:#800">#include</span><span style=3D"c=
olor:#000"> </span><span style=3D"color:#080"><cstdint></span><span s=
tyle=3D"color:#000"><br></span><span style=3D"color:#800">#include</span><s=
pan style=3D"color:#000"> </span><span style=3D"color:#080"><limits><=
/span><span style=3D"color:#000"><br></span><span style=3D"color:#800">#inc=
lude</span><span style=3D"color:#000"> </span><span style=3D"color:#080">&l=
t;iostream></span><span style=3D"color:#000"><br></span><span style=3D"c=
olor:#800">#include</span><span style=3D"color:#000"> </span><span style=3D=
"color:#080"><type_traits></span><span style=3D"color:#000"><br><br><=
/span><span style=3D"color:#008">template</span><span style=3D"color:#660">=
<</span><span style=3D"color:#008">typename</span><span style=3D"color:#=
000"> T</span><span style=3D"color:#660">></span><span style=3D"color:#0=
00"><br></span><span style=3D"color:#008">concept</span><span style=3D"colo=
r:#000"> </span><span style=3D"color:#008">bool</span><span style=3D"color:=
#000"> </span><span style=3D"color:#606">Integral</span><span style=3D"colo=
r:#000"> </span><span style=3D"color:#660">=3D</span><span style=3D"color:#=
000"> std</span><span style=3D"color:#660">::</span><span style=3D"color:#0=
00">is_integral</span><span style=3D"color:#660"><</span><span style=3D"=
color:#000">T</span><span style=3D"color:#660">>::</span><span style=3D"=
color:#000">value</span><span style=3D"color:#660">;</span><span style=3D"c=
olor:#000"><br><br></span><span style=3D"color:#008">template</span><span s=
tyle=3D"color:#660"><</span><span style=3D"color:#606">Integral</span><s=
pan style=3D"color:#000"> </span><span style=3D"color:#606">ResultType</spa=
n><span style=3D"color:#660">,</span><span style=3D"color:#000"> </span><sp=
an style=3D"color:#606">Integral</span><span style=3D"color:#000"> </span><=
span style=3D"color:#606">ValueType</span><span style=3D"color:#660">></=
span><span style=3D"color:#000"><br></span><span style=3D"color:#008">auto<=
/span><span style=3D"color:#000"> abs</span><span style=3D"color:#660">(</s=
pan><span style=3D"color:#008">const</span><span style=3D"color:#000"> </sp=
an><span style=3D"color:#606">ValueType</span><span style=3D"color:#000"> s=
</span><span style=3D"color:#660">)</span><span style=3D"color:#000"> </spa=
n><span style=3D"color:#660">-></span><span style=3D"color:#000"> </span=
><span style=3D"color:#606">ResultType</span><span style=3D"color:#000"><br=
></span><span style=3D"color:#660">{</span><span style=3D"color:#000"><br>=
=C2=A0 =C2=A0 </span><span style=3D"color:#008">static_assert</span><span s=
tyle=3D"color:#660">(</span><span style=3D"color:#008">sizeof</span><span s=
tyle=3D"color:#660">(</span><span style=3D"color:#606">ResultTyp<wbr>e</spa=
n><span style=3D"color:#660">)</span><span style=3D"color:#000"> </span><sp=
an style=3D"color:#660">></span><span style=3D"color:#000"> </span><span=
style=3D"color:#008">sizeof</span><span style=3D"color:#660">(</span><span=
style=3D"color:#008">decltype</span><span style=3D"color:#660">(</span><sp=
an style=3D"color:#000">s</span><span style=3D"color:#660">))</span><span s=
tyle=3D"color:#000"> </span><span style=3D"color:#660">||</span><span style=
=3D"color:#000"> std</span><span style=3D"color:#660">::</span><span style=
=3D"color:#000">is_same_v</span><span style=3D"color:#660"><</span><span=
style=3D"color:#606">ResultType</span><span style=3D"color:#660">,</span><=
span style=3D"color:#000"> std</span><span style=3D"color:#660">::</span><s=
pan style=3D"color:#000">make_unsigned_t</span><span style=3D"color:#660">&=
lt;</span><span style=3D"color:#606">ValueType</span><span style=3D"color:#=
660"><wbr>>>,</span><span style=3D"color:#000"> </span><span style=3D=
"color:#080">"The result type must be able to store the positive value=
!"</span><span style=3D"color:#660">);</span><span style=3D"color:#000=
"><br>=C2=A0 =C2=A0 </span><span style=3D"color:#606">ResultType</span><spa=
n style=3D"color:#000"> absVal</span><span style=3D"color:#660">;</span><sp=
an style=3D"color:#000"><br>=C2=A0 =C2=A0 <br>=C2=A0 =C2=A0 </span><span st=
yle=3D"color:#008">if</span><span style=3D"color:#660">(</span><span style=
=3D"color:#000"> s </span><span style=3D"color:#660">>=3D</span><span st=
yle=3D"color:#000"> </span><span style=3D"color:#066">0</span><span style=
=3D"color:#000"> </span><span style=3D"color:#660">)</span><span style=3D"c=
olor:#000"><br>=C2=A0 =C2=A0 </span><span style=3D"color:#660">{</span><spa=
n style=3D"color:#000"><br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 </span><span style=
=3D"color:#008">return</span><span style=3D"color:#000"> </span><span style=
=3D"color:#008">static_cast</span><span style=3D"color:#660"><</span><sp=
an style=3D"color:#606">ResultType</span><span style=3D"color:#660">>(</=
span><span style=3D"color:#000">s</span><span style=3D"color:#660">);</span=
><span style=3D"color:#000"><br>=C2=A0 =C2=A0 </span><span style=3D"color:#=
660">}</span><span style=3D"color:#000"><br>=C2=A0 =C2=A0 </span><span styl=
e=3D"color:#008">else</span><span style=3D"color:#000"><br>=C2=A0 =C2=A0 </=
span><span style=3D"color:#660">{</span><span style=3D"color:#000"><br>=C2=
=A0 =C2=A0 =C2=A0 =C2=A0 absVal </span><span style=3D"color:#660">=3D</span=
><span style=3D"color:#000"> </span><span style=3D"color:#660">~</span><spa=
n style=3D"color:#000">s</span><span style=3D"color:#660">;</span><span sty=
le=3D"color:#000"><br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 absVal</span><span style=
=3D"color:#660">++;</span><span style=3D"color:#000"><br>=C2=A0 =C2=A0 =C2=
=A0 =C2=A0 </span><span style=3D"color:#008">return</span><span style=3D"co=
lor:#000"> absVal</span><span style=3D"color:#660">;</span><span style=3D"c=
olor:#000"><br>=C2=A0 =C2=A0 </span><span style=3D"color:#660">}</span><spa=
n style=3D"color:#000"><br></span><span style=3D"color:#660">}</span><span =
style=3D"color:#000"><br><br></span><span style=3D"color:#008">int</span><s=
pan style=3D"color:#000"> main</span><span style=3D"color:#660">(</span><sp=
an style=3D"color:#008">void</span><span style=3D"color:#660">)</span><span=
style=3D"color:#000"><br></span><span style=3D"color:#660">{</span><span s=
tyle=3D"color:#000"><br>=C2=A0 =C2=A0 std</span><span style=3D"color:#660">=
::</span><span style=3D"color:#000">cout </span><span style=3D"color:#660">=
<<</span><span style=3D"color:#000"> abs</span><span style=3D"color:#=
660"><</span><span style=3D"color:#000">std</span><span style=3D"color:#=
660">::</span><span style=3D"color:#000">uint8_t</span><span style=3D"color=
:#660">>(</span><span style=3D"color:#000">std</span><span style=3D"colo=
r:#660">::</span><span style=3D"color:#000">numeric<wbr>_limits</span><span=
style=3D"color:#660"><</span><span style=3D"color:#000">std</span><span=
style=3D"color:#660">::</span><span style=3D"color:#000">int8_t</span><spa=
n style=3D"color:#660">>::</span><span style=3D"color:#000">min</span><s=
pan style=3D"color:#660">())</span><span style=3D"color:#000"> </span><span=
style=3D"color:#660"><<</span><span style=3D"color:#000"> </span><sp=
an style=3D"color:#080">'\n'</span><span style=3D"color:#660">;</sp=
an><span style=3D"color:#000"><br>=C2=A0 =C2=A0 std</span><span style=3D"co=
lor:#660">::</span><span style=3D"color:#000">cout </span><span style=3D"co=
lor:#660"><<</span><span style=3D"color:#000"> abs</span><span style=
=3D"color:#660"><</span><span style=3D"color:#000">std</span><span style=
=3D"color:#660">::</span><span style=3D"color:#000">int32_t</span><span sty=
le=3D"color:#660">>(</span><span style=3D"color:#000">std</span><span st=
yle=3D"color:#660">::</span><span style=3D"color:#000">numeric<wbr>_limits<=
/span><span style=3D"color:#660"><</span><span style=3D"color:#000">std<=
/span><span style=3D"color:#660">::</span><span style=3D"color:#000">int16_=
t</span><span style=3D"color:#660">>::</span><span style=3D"color:#000">=
min</span><span style=3D"color:#660">())</span><span style=3D"color:#000"> =
</span><span style=3D"color:#660"><<</span><span style=3D"color:#000"=
> </span><span style=3D"color:#080">'\n'</span><span style=3D"color=
:#660">;</span><span style=3D"color:#000"><br>=C2=A0 =C2=A0 </span><span st=
yle=3D"color:#800">//std::cout << abs<std::uint8_t>(std::<wbr>n=
umeric_limits<std::int64_t>::<wbr>min()) << '\n';</span=
><span style=3D"color:#000"><br>=C2=A0 =C2=A0 </span><span style=3D"color:#=
008">return</span><span style=3D"color:#000"> </span><span style=3D"color:#=
066">0</span><span style=3D"color:#660">;</span><span style=3D"color:#000">=
<br></span><span style=3D"color:#660">}</span></div></code></div><br>it can=
be named abs, because as the result type must be specified it is different=
from the existing abs overloads and thus does not need a new name.</div><d=
iv>So I agree to abs<unsigned>();</div><div>And if you do not care ab=
out safety you could even leave out the static_assert. We could also make t=
he return type be any arithmetic type.<br></div><div>In this case floor<=
int>(), ceil<int>() and round<int>() and maybe other similar=
functions could also be added.</div><br>Am Sonntag, 14. Oktober 2018 17:18=
:09 UTC+2 schrieb John McFarlane:<blockquote class=3D"gmail_quote" style=3D=
"margin:0;margin-left:0.8ex;border-left:1px #ccc solid;padding-left:1ex"><b=
r><br><div class=3D"gmail_quote"><div dir=3D"ltr">On Sun, Oct 14, 2018, 03:=
31 Bengt Gustafsson <<a rel=3D"nofollow">bengt.gu...@beamways.com</a>>=
; wrote:<br></div><blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .=
8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir=3D"ltr">For me th=
e motivation for uabs() would more be to avoid unsigned/signed mismatch err=
ors by getting the result as an unsigned (which we know it conceptually is =
after abs).</div></blockquote></div><div><br></div><div>I consider a number=
being positive as different to a number being unsigned. I'd rather tho=
se two things (conversion and absolute value)=C2=A0remained separate concer=
ns.<br></div><div class=3D"gmail_quote"><blockquote class=3D"gmail_quote" s=
tyle=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div=
dir=3D"ltr"><div><br></div><div>This is very similar to the annoyance of f=
loor, round and ceil returning floats which we know have integer values, fo=
rcing us to write int(round(x)) when using this with other ints to avoid co=
mpiler warnings. I think I saw something about new floor<int>() and s=
imilar functions being proposed, so why not uabs?</div></div></blockquote><=
/div><div><br></div><div>I'm less adverse to abs<unsigned>() beca=
use the two concerns are more apparent but only slightly.=C2=A0</div><div><=
br></div><div>If you're converting a float to an int, a necessary detai=
l is how to approximate fractional values. So a convert<int, floor_tag&g=
t;() starts to become palatable. p0105 proposed something similar. I'm =
not familiar with the floor function you mention.</div><div class=3D"gmail_=
quote"><blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-=
left:1px #ccc solid;padding-left:1ex"><div dir=3D"ltr"><div><br></div></div=
>
<p></p>
-- <br>
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" group.<br>
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a rel=3D"nofollow">std-proposal...@isocpp.org</a>.<br>
To post to this group, send email to <a rel=3D"nofollow">std-pr...@isocpp.o=
rg</a>.<br>
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/3a5821ae-c386-46b4-8e73-4bff8764679c%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter" rel=3D"nofollow" t=
arget=3D"_blank" onmousedown=3D"this.href=3D'https://groups.google.com/=
a/isocpp.org/d/msgid/std-proposals/3a5821ae-c386-46b4-8e73-4bff8764679c%40i=
socpp.org?utm_medium\x3demail\x26utm_source\x3dfooter';return true;" on=
click=3D"this.href=3D'https://groups.google.com/a/isocpp.org/d/msgid/st=
d-proposals/3a5821ae-c386-46b4-8e73-4bff8764679c%40isocpp.org?utm_medium\x3=
demail\x26utm_source\x3dfooter';return true;">https://groups.google.com=
/a/<wbr>isocpp.org/d/msgid/std-<wbr>proposals/3a5821ae-c386-46b4-<wbr>8e73-=
4bff8764679c%40isocpp.org</a><wbr>.<br>
</blockquote></div>
</blockquote></div></blockquote></div>
<p></p>
-- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/9d74ddbb-cc68-412a-ae6b-4664660ff12c%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/9d74ddbb-cc68-412a-ae6b-4664660ff12c=
%40isocpp.org</a>.<br />
------=_Part_4136_1414481924.1540034851854--
------=_Part_4135_1304879855.1540034851853--
.
Author: Bryce Adelstein Lelbach aka wash <brycelelbach@gmail.com>
Date: Sat, 20 Oct 2018 16:00:38 -0400
Raw View
--00000000000002d6130578ae7a99
Content-Type: text/plain; charset="UTF-8"
JF? (See question below)
On Sat, Oct 20, 2018, 7:27 AM <burningorca@gmail.com> wrote:
> I recently read that their is a paper suggesting integers always being
> twos complement should be standardised. If this is the case, would the call
> to the existing std::abs(std::numeric_limits<SignedType>::min()) become
> well defined behaviour?
>
> Am Samstag, 20. Oktober 2018 13:25:19 UTC+2 schrieb burni...@gmail.com:
>>
>> But the result of round, ceil and float may not result in a value that is
>> able to be stored in an integer. The result of abs can always be stored in
>> unsigned same type or any greater type.
>> abs, floor, ceil and round are all C-function which do not have the power
>> of templates.
>>
>> But if we would have something like:
>>
>> #include <cstdint>
>> #include <limits>
>> #include <iostream>
>> #include <type_traits>
>>
>> template<typename T>
>> concept bool Integral = std::is_integral<T>::value;
>>
>> template<Integral ResultType, Integral ValueType>
>> auto abs(const ValueType s) -> ResultType
>> {
>> static_assert(sizeof(ResultType) > sizeof(decltype(s)) || std::
>> is_same_v<ResultType, std::make_unsigned_t<ValueType>>, "The result type
>> must be able to store the positive value!");
>> ResultType absVal;
>>
>> if( s >= 0 )
>> {
>> return static_cast<ResultType>(s);
>> }
>> else
>> {
>> absVal = ~s;
>> absVal++;
>> return absVal;
>> }
>> }
>>
>> int main(void)
>> {
>> std::cout << abs<std::uint8_t>(std::numeric_limits<std::int8_t>::min
>> ()) << '\n';
>> std::cout << abs<std::int32_t>(std::numeric_limits<std::int16_t>::min
>> ()) << '\n';
>> //std::cout <<
>> abs<std::uint8_t>(std::numeric_limits<std::int64_t>::min()) << '\n';
>> return 0;
>> }
>>
>> it can be named abs, because as the result type must be specified it is
>> different from the existing abs overloads and thus does not need a new name.
>> So I agree to abs<unsigned>();
>> And if you do not care about safety you could even leave out the
>> static_assert. We could also make the return type be any arithmetic type.
>> In this case floor<int>(), ceil<int>() and round<int>() and maybe other
>> similar functions could also be added.
>>
>> Am Sonntag, 14. Oktober 2018 17:18:09 UTC+2 schrieb John McFarlane:
>>>
>>>
>>>
>>> On Sun, Oct 14, 2018, 03:31 Bengt Gustafsson <bengt.gu...@beamways.com>
>>> wrote:
>>>
>>>> For me the motivation for uabs() would more be to avoid unsigned/signed
>>>> mismatch errors by getting the result as an unsigned (which we know it
>>>> conceptually is after abs).
>>>>
>>>
>>> I consider a number being positive as different to a number being
>>> unsigned. I'd rather those two things (conversion and absolute
>>> value) remained separate concerns.
>>>
>>>>
>>>> This is very similar to the annoyance of floor, round and ceil
>>>> returning floats which we know have integer values, forcing us to write
>>>> int(round(x)) when using this with other ints to avoid compiler warnings. I
>>>> think I saw something about new floor<int>() and similar functions being
>>>> proposed, so why not uabs?
>>>>
>>>
>>> I'm less adverse to abs<unsigned>() because the two concerns are more
>>> apparent but only slightly.
>>>
>>> If you're converting a float to an int, a necessary detail is how to
>>> approximate fractional values. So a convert<int, floor_tag>() starts to
>>> become palatable. p0105 proposed something similar. I'm not familiar with
>>> the floor function you mention.
>>>
>>>>
>>>> --
>>>> You received this message because you are subscribed to the Google
>>>> Groups "ISO C++ Standard - Future Proposals" group.
>>>> To unsubscribe from this group and stop receiving emails from it, send
>>>> an email to std-proposal...@isocpp.org.
>>>> To post to this group, send email to std-pr...@isocpp.org.
>>>> To view this discussion on the web visit
>>>> https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/3a5821ae-c386-46b4-8e73-4bff8764679c%40isocpp.org
>>>> <https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/3a5821ae-c386-46b4-8e73-4bff8764679c%40isocpp.org?utm_medium=email&utm_source=footer>
>>>> .
>>>>
>>> --
> You received this message because you are subscribed to the Google Groups
> "ISO C++ Standard - Future Proposals" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to std-proposals+unsubscribe@isocpp.org.
> To post to this group, send email to std-proposals@isocpp.org.
> To view this discussion on the web visit
> https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/9d74ddbb-cc68-412a-ae6b-4664660ff12c%40isocpp.org
> <https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/9d74ddbb-cc68-412a-ae6b-4664660ff12c%40isocpp.org?utm_medium=email&utm_source=footer>
> .
>
--
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CAP3wax_-GqPOxaDyd1QKx40Yvj6%3DMqDn6-1cYrtUw7ObzuJcMg%40mail.gmail.com.
--00000000000002d6130578ae7a99
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
<div dir=3D"auto">JF? (See question below)</div><br><div class=3D"gmail_quo=
te"><div dir=3D"ltr">On Sat, Oct 20, 2018, 7:27 AM <<a href=3D"mailto:b=
urningorca@gmail.com">burningorca@gmail.com</a>> wrote:<br></div><blockq=
uote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc =
solid;padding-left:1ex"><div dir=3D"ltr">I recently read that their is a pa=
per suggesting integers always being twos complement should be standardised=
.. If this is the case, would the call to the existing std::abs(std::numeric=
_limits<SignedType>::min()) become well defined behaviour?<br><br>Am =
Samstag, 20. Oktober 2018 13:25:19 UTC+2 schrieb <a href=3D"mailto:burni...=
@gmail.com" target=3D"_blank" rel=3D"noreferrer">burni...@gmail.com</a>:<bl=
ockquote class=3D"gmail_quote" style=3D"margin:0;margin-left:0.8ex;border-l=
eft:1px #ccc solid;padding-left:1ex"><div dir=3D"ltr"><div>But the result o=
f round, ceil and float may not result in a value that is able to be stored=
in an integer. The result of abs can always be stored in=C2=A0 unsigned sa=
me type or any greater type.</div><div>abs, floor, ceil and round are all C=
-function which do not have the power of templates.</div><div><br></div><di=
v>But if we would have something like:</div><div><br></div><div><div style=
=3D"background-color:rgb(250,250,250);border-color:rgb(187,187,187);border-=
style:solid;border-width:1px"><code><div><span style=3D"color:#800">#includ=
e</span><span style=3D"color:#000"> </span><span style=3D"color:#080"><c=
stdint></span><span style=3D"color:#000"><br></span><span style=3D"color=
:#800">#include</span><span style=3D"color:#000"> </span><span style=3D"col=
or:#080"><limits></span><span style=3D"color:#000"><br></span><span s=
tyle=3D"color:#800">#include</span><span style=3D"color:#000"> </span><span=
style=3D"color:#080"><iostream></span><span style=3D"color:#000"><br=
></span><span style=3D"color:#800">#include</span><span style=3D"color:#000=
"> </span><span style=3D"color:#080"><type_traits></span><span style=
=3D"color:#000"><br><br></span><span style=3D"color:#008">template</span><s=
pan style=3D"color:#660"><</span><span style=3D"color:#008">typename</sp=
an><span style=3D"color:#000"> T</span><span style=3D"color:#660">></spa=
n><span style=3D"color:#000"><br></span><span style=3D"color:#008">concept<=
/span><span style=3D"color:#000"> </span><span style=3D"color:#008">bool</s=
pan><span style=3D"color:#000"> </span><span style=3D"color:#606">Integral<=
/span><span style=3D"color:#000"> </span><span style=3D"color:#660">=3D</sp=
an><span style=3D"color:#000"> std</span><span style=3D"color:#660">::</spa=
n><span style=3D"color:#000">is_integral</span><span style=3D"color:#660">&=
lt;</span><span style=3D"color:#000">T</span><span style=3D"color:#660">>=
;::</span><span style=3D"color:#000">value</span><span style=3D"color:#660"=
>;</span><span style=3D"color:#000"><br><br></span><span style=3D"color:#00=
8">template</span><span style=3D"color:#660"><</span><span style=3D"colo=
r:#606">Integral</span><span style=3D"color:#000"> </span><span style=3D"co=
lor:#606">ResultType</span><span style=3D"color:#660">,</span><span style=
=3D"color:#000"> </span><span style=3D"color:#606">Integral</span><span sty=
le=3D"color:#000"> </span><span style=3D"color:#606">ValueType</span><span =
style=3D"color:#660">></span><span style=3D"color:#000"><br></span><span=
style=3D"color:#008">auto</span><span style=3D"color:#000"> abs</span><spa=
n style=3D"color:#660">(</span><span style=3D"color:#008">const</span><span=
style=3D"color:#000"> </span><span style=3D"color:#606">ValueType</span><s=
pan style=3D"color:#000"> s</span><span style=3D"color:#660">)</span><span =
style=3D"color:#000"> </span><span style=3D"color:#660">-></span><span s=
tyle=3D"color:#000"> </span><span style=3D"color:#606">ResultType</span><sp=
an style=3D"color:#000"><br></span><span style=3D"color:#660">{</span><span=
style=3D"color:#000"><br>=C2=A0 =C2=A0 </span><span style=3D"color:#008">s=
tatic_assert</span><span style=3D"color:#660">(</span><span style=3D"color:=
#008">sizeof</span><span style=3D"color:#660">(</span><span style=3D"color:=
#606">ResultType</span><span style=3D"color:#660">)</span><span style=3D"co=
lor:#000"> </span><span style=3D"color:#660">></span><span style=3D"colo=
r:#000"> </span><span style=3D"color:#008">sizeof</span><span style=3D"colo=
r:#660">(</span><span style=3D"color:#008">decltype</span><span style=3D"co=
lor:#660">(</span><span style=3D"color:#000">s</span><span style=3D"color:#=
660">))</span><span style=3D"color:#000"> </span><span style=3D"color:#660"=
>||</span><span style=3D"color:#000"> std</span><span style=3D"color:#660">=
::</span><span style=3D"color:#000">is_same_v</span><span style=3D"color:#6=
60"><</span><span style=3D"color:#606">ResultType</span><span style=3D"c=
olor:#660">,</span><span style=3D"color:#000"> std</span><span style=3D"col=
or:#660">::</span><span style=3D"color:#000">make_unsigned_t</span><span st=
yle=3D"color:#660"><</span><span style=3D"color:#606">ValueType</span><s=
pan style=3D"color:#660">>>,</span><span style=3D"color:#000"> </span=
><span style=3D"color:#080">"The result type must be able to store the=
positive value!"</span><span style=3D"color:#660">);</span><span styl=
e=3D"color:#000"><br>=C2=A0 =C2=A0 </span><span style=3D"color:#606">Result=
Type</span><span style=3D"color:#000"> absVal</span><span style=3D"color:#6=
60">;</span><span style=3D"color:#000"><br>=C2=A0 =C2=A0 <br>=C2=A0 =C2=A0 =
</span><span style=3D"color:#008">if</span><span style=3D"color:#660">(</sp=
an><span style=3D"color:#000"> s </span><span style=3D"color:#660">>=3D<=
/span><span style=3D"color:#000"> </span><span style=3D"color:#066">0</span=
><span style=3D"color:#000"> </span><span style=3D"color:#660">)</span><spa=
n style=3D"color:#000"><br>=C2=A0 =C2=A0 </span><span style=3D"color:#660">=
{</span><span style=3D"color:#000"><br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 </span><=
span style=3D"color:#008">return</span><span style=3D"color:#000"> </span><=
span style=3D"color:#008">static_cast</span><span style=3D"color:#660"><=
</span><span style=3D"color:#606">ResultType</span><span style=3D"color:#66=
0">>(</span><span style=3D"color:#000">s</span><span style=3D"color:#660=
">);</span><span style=3D"color:#000"><br>=C2=A0 =C2=A0 </span><span style=
=3D"color:#660">}</span><span style=3D"color:#000"><br>=C2=A0 =C2=A0 </span=
><span style=3D"color:#008">else</span><span style=3D"color:#000"><br>=C2=
=A0 =C2=A0 </span><span style=3D"color:#660">{</span><span style=3D"color:#=
000"><br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 absVal </span><span style=3D"color:#66=
0">=3D</span><span style=3D"color:#000"> </span><span style=3D"color:#660">=
~</span><span style=3D"color:#000">s</span><span style=3D"color:#660">;</sp=
an><span style=3D"color:#000"><br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 absVal</span>=
<span style=3D"color:#660">++;</span><span style=3D"color:#000"><br>=C2=A0 =
=C2=A0 =C2=A0 =C2=A0 </span><span style=3D"color:#008">return</span><span s=
tyle=3D"color:#000"> absVal</span><span style=3D"color:#660">;</span><span =
style=3D"color:#000"><br>=C2=A0 =C2=A0 </span><span style=3D"color:#660">}<=
/span><span style=3D"color:#000"><br></span><span style=3D"color:#660">}</s=
pan><span style=3D"color:#000"><br><br></span><span style=3D"color:#008">in=
t</span><span style=3D"color:#000"> main</span><span style=3D"color:#660">(=
</span><span style=3D"color:#008">void</span><span style=3D"color:#660">)</=
span><span style=3D"color:#000"><br></span><span style=3D"color:#660">{</sp=
an><span style=3D"color:#000"><br>=C2=A0 =C2=A0 std</span><span style=3D"co=
lor:#660">::</span><span style=3D"color:#000">cout </span><span style=3D"co=
lor:#660"><<</span><span style=3D"color:#000"> abs</span><span style=
=3D"color:#660"><</span><span style=3D"color:#000">std</span><span style=
=3D"color:#660">::</span><span style=3D"color:#000">uint8_t</span><span sty=
le=3D"color:#660">>(</span><span style=3D"color:#000">std</span><span st=
yle=3D"color:#660">::</span><span style=3D"color:#000">numeric_limits</span=
><span style=3D"color:#660"><</span><span style=3D"color:#000">std</span=
><span style=3D"color:#660">::</span><span style=3D"color:#000">int8_t</spa=
n><span style=3D"color:#660">>::</span><span style=3D"color:#000">min</s=
pan><span style=3D"color:#660">())</span><span style=3D"color:#000"> </span=
><span style=3D"color:#660"><<</span><span style=3D"color:#000"> </sp=
an><span style=3D"color:#080">'\n'</span><span style=3D"color:#660"=
>;</span><span style=3D"color:#000"><br>=C2=A0 =C2=A0 std</span><span style=
=3D"color:#660">::</span><span style=3D"color:#000">cout </span><span style=
=3D"color:#660"><<</span><span style=3D"color:#000"> abs</span><span =
style=3D"color:#660"><</span><span style=3D"color:#000">std</span><span =
style=3D"color:#660">::</span><span style=3D"color:#000">int32_t</span><spa=
n style=3D"color:#660">>(</span><span style=3D"color:#000">std</span><sp=
an style=3D"color:#660">::</span><span style=3D"color:#000">numeric_limits<=
/span><span style=3D"color:#660"><</span><span style=3D"color:#000">std<=
/span><span style=3D"color:#660">::</span><span style=3D"color:#000">int16_=
t</span><span style=3D"color:#660">>::</span><span style=3D"color:#000">=
min</span><span style=3D"color:#660">())</span><span style=3D"color:#000"> =
</span><span style=3D"color:#660"><<</span><span style=3D"color:#000"=
> </span><span style=3D"color:#080">'\n'</span><span style=3D"color=
:#660">;</span><span style=3D"color:#000"><br>=C2=A0 =C2=A0 </span><span st=
yle=3D"color:#800">//std::cout << abs<std::uint8_t>(std::numeri=
c_limits<std::int64_t>::min()) << '\n';</span><span sty=
le=3D"color:#000"><br>=C2=A0 =C2=A0 </span><span style=3D"color:#008">retur=
n</span><span style=3D"color:#000"> </span><span style=3D"color:#066">0</sp=
an><span style=3D"color:#660">;</span><span style=3D"color:#000"><br></span=
><span style=3D"color:#660">}</span></div></code></div><br>it can be named =
abs, because as the result type must be specified it is different from the =
existing abs overloads and thus does not need a new name.</div><div>So I ag=
ree to abs<unsigned>();</div><div>And if you do not care about safety=
you could even leave out the static_assert. We could also make the return =
type be any arithmetic type.<br></div><div>In this case floor<int>(),=
ceil<int>() and round<int>() and maybe other similar functions=
could also be added.</div><br>Am Sonntag, 14. Oktober 2018 17:18:09 UTC+2 =
schrieb John McFarlane:<blockquote class=3D"gmail_quote" style=3D"margin:0;=
margin-left:0.8ex;border-left:1px #ccc solid;padding-left:1ex"><br><br><div=
class=3D"gmail_quote"><div dir=3D"ltr">On Sun, Oct 14, 2018, 03:31 Bengt G=
ustafsson <<a rel=3D"nofollow noreferrer">bengt.gu...@beamways.com</a>&g=
t; wrote:<br></div><blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 =
..8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir=3D"ltr">For me t=
he motivation for uabs() would more be to avoid unsigned/signed mismatch er=
rors by getting the result as an unsigned (which we know it conceptually is=
after abs).</div></blockquote></div><div><br></div><div>I consider a numbe=
r being positive as different to a number being unsigned. I'd rather th=
ose two things (conversion and absolute value)=C2=A0remained separate conce=
rns.<br></div><div class=3D"gmail_quote"><blockquote class=3D"gmail_quote" =
style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><di=
v dir=3D"ltr"><div><br></div><div>This is very similar to the annoyance of =
floor, round and ceil returning floats which we know have integer values, f=
orcing us to write int(round(x)) when using this with other ints to avoid c=
ompiler warnings. I think I saw something about new floor<int>() and =
similar functions being proposed, so why not uabs?</div></div></blockquote>=
</div><div><br></div><div>I'm less adverse to abs<unsigned>() bec=
ause the two concerns are more apparent but only slightly.=C2=A0</div><div>=
<br></div><div>If you're converting a float to an int, a necessary deta=
il is how to approximate fractional values. So a convert<int, floor_tag&=
gt;() starts to become palatable. p0105 proposed something similar. I'm=
not familiar with the floor function you mention.</div><div class=3D"gmail=
_quote"><blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border=
-left:1px #ccc solid;padding-left:1ex"><div dir=3D"ltr"><div><br></div></di=
v>
<p></p>
-- <br>
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" group.<br>
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a rel=3D"nofollow noreferrer">std-proposal...@isocpp.org</a>.<br>
To post to this group, send email to <a rel=3D"nofollow noreferrer">std-pr.=
...@isocpp.org</a>.<br>
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/3a5821ae-c386-46b4-8e73-4bff8764679c%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter" rel=3D"nofollow no=
referrer" target=3D"_blank">https://groups.google.com/a/isocpp.org/d/msgid/=
std-proposals/3a5821ae-c386-46b4-8e73-4bff8764679c%40isocpp.org</a>.<br>
</blockquote></div>
</blockquote></div></blockquote></div>
<p></p>
-- <br>
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" group.<br>
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org" target=3D"_=
blank" rel=3D"noreferrer">std-proposals+unsubscribe@isocpp.org</a>.<br>
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org" target=3D"_blank" rel=3D"noreferrer">std-proposals@isocpp.org</a>.<br=
>
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/9d74ddbb-cc68-412a-ae6b-4664660ff12c%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter" target=3D"_blank" =
rel=3D"noreferrer">https://groups.google.com/a/isocpp.org/d/msgid/std-propo=
sals/9d74ddbb-cc68-412a-ae6b-4664660ff12c%40isocpp.org</a>.<br>
</blockquote></div>
<p></p>
-- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/CAP3wax_-GqPOxaDyd1QKx40Yvj6%3DMqDn6-=
1cYrtUw7ObzuJcMg%40mail.gmail.com?utm_medium=3Demail&utm_source=3Dfooter">h=
ttps://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CAP3wax_-GqPOxa=
Dyd1QKx40Yvj6%3DMqDn6-1cYrtUw7ObzuJcMg%40mail.gmail.com</a>.<br />
--00000000000002d6130578ae7a99--
.
Author: JF Bastien <cxx@jfbastien.com>
Date: Sat, 20 Oct 2018 15:52:29 -0700
Raw View
--0000000000009548b10578b0e0e0
Content-Type: text/plain; charset="UTF-8"
On Sat, Oct 20, 2018 at 1:00 PM Bryce Adelstein Lelbach aka wash <
brycelelbach@gmail.com> wrote:
> JF? (See question below)
>
> On Sat, Oct 20, 2018, 7:27 AM <burningorca@gmail.com> wrote:
>
>> I recently read that their is a paper suggesting integers always being
>> twos complement should be standardised. If this is the case, would the call
>> to the existing std::abs(std::numeric_limits<SignedType>::min()) become
>> well defined behaviour?
>>
>
http://wg21.link/p0907
*Status-quo* If a signed operation would naturally produce a value that is
not within the range of the result type, the behavior is undefined. The
author had hoped to make this well-defined as wrapping (the operations
produce the same value bits as for the corresponding unsigned type), but
WG21 had strong resistance against this.
> Am Samstag, 20. Oktober 2018 13:25:19 UTC+2 schrieb burni...@gmail.com:
>>>
>>> But the result of round, ceil and float may not result in a value that
>>> is able to be stored in an integer. The result of abs can always be stored
>>> in unsigned same type or any greater type.
>>> abs, floor, ceil and round are all C-function which do not have the
>>> power of templates.
>>>
>>> But if we would have something like:
>>>
>>> #include <cstdint>
>>> #include <limits>
>>> #include <iostream>
>>> #include <type_traits>
>>>
>>> template<typename T>
>>> concept bool Integral = std::is_integral<T>::value;
>>>
>>> template<Integral ResultType, Integral ValueType>
>>> auto abs(const ValueType s) -> ResultType
>>> {
>>> static_assert(sizeof(ResultType) > sizeof(decltype(s)) || std::
>>> is_same_v<ResultType, std::make_unsigned_t<ValueType>>, "The result
>>> type must be able to store the positive value!");
>>> ResultType absVal;
>>>
>>> if( s >= 0 )
>>> {
>>> return static_cast<ResultType>(s);
>>> }
>>> else
>>> {
>>> absVal = ~s;
>>> absVal++;
>>> return absVal;
>>> }
>>> }
>>>
>>> int main(void)
>>> {
>>> std::cout << abs<std::uint8_t>(std::numeric_limits<std::int8_t>::min
>>> ()) << '\n';
>>> std::cout << abs<std::int32_t>(std::numeric_limits<std::int16_t>::
>>> min()) << '\n';
>>> //std::cout <<
>>> abs<std::uint8_t>(std::numeric_limits<std::int64_t>::min()) << '\n';
>>> return 0;
>>> }
>>>
>>> it can be named abs, because as the result type must be specified it is
>>> different from the existing abs overloads and thus does not need a new name.
>>> So I agree to abs<unsigned>();
>>> And if you do not care about safety you could even leave out the
>>> static_assert. We could also make the return type be any arithmetic type.
>>> In this case floor<int>(), ceil<int>() and round<int>() and maybe other
>>> similar functions could also be added.
>>>
>>> Am Sonntag, 14. Oktober 2018 17:18:09 UTC+2 schrieb John McFarlane:
>>>>
>>>>
>>>>
>>>> On Sun, Oct 14, 2018, 03:31 Bengt Gustafsson <bengt.gu...@beamways.com>
>>>> wrote:
>>>>
>>>>> For me the motivation for uabs() would more be to avoid
>>>>> unsigned/signed mismatch errors by getting the result as an unsigned (which
>>>>> we know it conceptually is after abs).
>>>>>
>>>>
>>>> I consider a number being positive as different to a number being
>>>> unsigned. I'd rather those two things (conversion and absolute
>>>> value) remained separate concerns.
>>>>
>>>>>
>>>>> This is very similar to the annoyance of floor, round and ceil
>>>>> returning floats which we know have integer values, forcing us to write
>>>>> int(round(x)) when using this with other ints to avoid compiler warnings. I
>>>>> think I saw something about new floor<int>() and similar functions being
>>>>> proposed, so why not uabs?
>>>>>
>>>>
>>>> I'm less adverse to abs<unsigned>() because the two concerns are more
>>>> apparent but only slightly.
>>>>
>>>> If you're converting a float to an int, a necessary detail is how to
>>>> approximate fractional values. So a convert<int, floor_tag>() starts to
>>>> become palatable. p0105 proposed something similar. I'm not familiar with
>>>> the floor function you mention.
>>>>
>>>>>
>>>>> --
>>>>> You received this message because you are subscribed to the Google
>>>>> Groups "ISO C++ Standard - Future Proposals" group.
>>>>> To unsubscribe from this group and stop receiving emails from it, send
>>>>> an email to std-proposal...@isocpp.org.
>>>>> To post to this group, send email to std-pr...@isocpp.org.
>>>>> To view this discussion on the web visit
>>>>> https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/3a5821ae-c386-46b4-8e73-4bff8764679c%40isocpp.org
>>>>> <https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/3a5821ae-c386-46b4-8e73-4bff8764679c%40isocpp.org?utm_medium=email&utm_source=footer>
>>>>> .
>>>>>
>>>> --
>> You received this message because you are subscribed to the Google Groups
>> "ISO C++ Standard - Future Proposals" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to std-proposals+unsubscribe@isocpp.org.
>> To post to this group, send email to std-proposals@isocpp.org.
>> To view this discussion on the web visit
>> https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/9d74ddbb-cc68-412a-ae6b-4664660ff12c%40isocpp.org
>> <https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/9d74ddbb-cc68-412a-ae6b-4664660ff12c%40isocpp.org?utm_medium=email&utm_source=footer>
>> .
>>
>
--
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CACeCrotMrChHSoswwYpxqRfd9OR2Fsfoz05RwOm3w%2BaEBqEGQw%40mail.gmail.com.
--0000000000009548b10578b0e0e0
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr"><div dir=3D"ltr"><br><br><div class=3D"gmail_quote"><div d=
ir=3D"ltr">On Sat, Oct 20, 2018 at 1:00 PM Bryce Adelstein Lelbach aka wash=
<<a href=3D"mailto:brycelelbach@gmail.com">brycelelbach@gmail.com</a>&g=
t; wrote:<br></div><blockquote class=3D"gmail_quote" style=3D"margin:0px 0p=
x 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div d=
ir=3D"auto">JF? (See question below)</div><br><div class=3D"gmail_quote"><d=
iv dir=3D"ltr">On Sat, Oct 20, 2018, 7:27 AM <<a href=3D"mailto:burning=
orca@gmail.com" target=3D"_blank">burningorca@gmail.com</a>> wrote:<br><=
/div><blockquote class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;bo=
rder-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir=3D"ltr">I r=
ecently read that their is a paper suggesting integers always being twos co=
mplement should be standardised. If this is the case, would the call to the=
existing std::abs(std::numeric_limits<SignedType>::min()) become wel=
l defined behaviour?<br></div></blockquote></div></blockquote><div><br></di=
v><div><a href=3D"http://wg21.link/p0907">http://wg21.link/p0907<br></a></d=
iv><div><br></div></div></div><blockquote style=3D"margin:0 0 0 40px;border=
:none;padding:0px"><div dir=3D"ltr"><div class=3D"gmail_quote"><div><em sty=
le=3D"color:rgb(0,0,0);font-family:sans-serif;font-size:medium">Status-quo<=
/em><span style=3D"color:rgb(0,0,0);font-family:sans-serif;font-size:medium=
">=C2=A0If a signed operation would naturally produce a value that is not w=
ithin the range of the result type, the behavior is undefined. The author h=
ad hoped to make this well-defined as wrapping (the operations produce the =
same value bits as for the corresponding unsigned type), but WG21 had stron=
g resistance against this.</span></div></div></div></blockquote><div dir=3D=
"ltr"><div class=3D"gmail_quote"><div><br></div><div><br></div><div>=C2=A0<=
/div><blockquote class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;bo=
rder-left:1px solid rgb(204,204,204);padding-left:1ex"><div class=3D"gmail_=
quote"><blockquote class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;=
border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir=3D"ltr">A=
m Samstag, 20. Oktober 2018 13:25:19 UTC+2 schrieb <a href=3D"mailto:burni.=
...@gmail.com" rel=3D"noreferrer" target=3D"_blank">burni...@gmail.com</a>:<=
blockquote class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;border-l=
eft:1px solid rgb(204,204,204);padding-left:1ex"><div dir=3D"ltr"><div>But =
the result of round, ceil and float may not result in a value that is able =
to be stored in an integer. The result of abs can always be stored in=C2=A0=
unsigned same type or any greater type.</div><div>abs, floor, ceil and rou=
nd are all C-function which do not have the power of templates.</div><div><=
br></div><div>But if we would have something like:</div><div><br></div><div=
><div style=3D"background-color:rgb(250,250,250);border-color:rgb(187,187,1=
87);border-style:solid;border-width:1px"><code><div><span style=3D"color:rg=
b(136,0,0)">#include</span><span style=3D"color:rgb(0,0,0)"> </span><span s=
tyle=3D"color:rgb(0,136,0)"><cstdint></span><span style=3D"color:rgb(=
0,0,0)"><br></span><span style=3D"color:rgb(136,0,0)">#include</span><span =
style=3D"color:rgb(0,0,0)"> </span><span style=3D"color:rgb(0,136,0)"><l=
imits></span><span style=3D"color:rgb(0,0,0)"><br></span><span style=3D"=
color:rgb(136,0,0)">#include</span><span style=3D"color:rgb(0,0,0)"> </span=
><span style=3D"color:rgb(0,136,0)"><iostream></span><span style=3D"c=
olor:rgb(0,0,0)"><br></span><span style=3D"color:rgb(136,0,0)">#include</sp=
an><span style=3D"color:rgb(0,0,0)"> </span><span style=3D"color:rgb(0,136,=
0)"><type_traits></span><span style=3D"color:rgb(0,0,0)"><br><br></sp=
an><span style=3D"color:rgb(0,0,136)">template</span><span style=3D"color:r=
gb(102,102,0)"><</span><span style=3D"color:rgb(0,0,136)">typename</span=
><span style=3D"color:rgb(0,0,0)"> T</span><span style=3D"color:rgb(102,102=
,0)">></span><span style=3D"color:rgb(0,0,0)"><br></span><span style=3D"=
color:rgb(0,0,136)">concept</span><span style=3D"color:rgb(0,0,0)"> </span>=
<span style=3D"color:rgb(0,0,136)">bool</span><span style=3D"color:rgb(0,0,=
0)"> </span><span style=3D"color:rgb(102,0,102)">Integral</span><span style=
=3D"color:rgb(0,0,0)"> </span><span style=3D"color:rgb(102,102,0)">=3D</spa=
n><span style=3D"color:rgb(0,0,0)"> std</span><span style=3D"color:rgb(102,=
102,0)">::</span><span style=3D"color:rgb(0,0,0)">is_integral</span><span s=
tyle=3D"color:rgb(102,102,0)"><</span><span style=3D"color:rgb(0,0,0)">T=
</span><span style=3D"color:rgb(102,102,0)">>::</span><span style=3D"col=
or:rgb(0,0,0)">value</span><span style=3D"color:rgb(102,102,0)">;</span><sp=
an style=3D"color:rgb(0,0,0)"><br><br></span><span style=3D"color:rgb(0,0,1=
36)">template</span><span style=3D"color:rgb(102,102,0)"><</span><span s=
tyle=3D"color:rgb(102,0,102)">Integral</span><span style=3D"color:rgb(0,0,0=
)"> </span><span style=3D"color:rgb(102,0,102)">ResultType</span><span styl=
e=3D"color:rgb(102,102,0)">,</span><span style=3D"color:rgb(0,0,0)"> </span=
><span style=3D"color:rgb(102,0,102)">Integral</span><span style=3D"color:r=
gb(0,0,0)"> </span><span style=3D"color:rgb(102,0,102)">ValueType</span><sp=
an style=3D"color:rgb(102,102,0)">></span><span style=3D"color:rgb(0,0,0=
)"><br></span><span style=3D"color:rgb(0,0,136)">auto</span><span style=3D"=
color:rgb(0,0,0)"> abs</span><span style=3D"color:rgb(102,102,0)">(</span><=
span style=3D"color:rgb(0,0,136)">const</span><span style=3D"color:rgb(0,0,=
0)"> </span><span style=3D"color:rgb(102,0,102)">ValueType</span><span styl=
e=3D"color:rgb(0,0,0)"> s</span><span style=3D"color:rgb(102,102,0)">)</spa=
n><span style=3D"color:rgb(0,0,0)"> </span><span style=3D"color:rgb(102,102=
,0)">-></span><span style=3D"color:rgb(0,0,0)"> </span><span style=3D"co=
lor:rgb(102,0,102)">ResultType</span><span style=3D"color:rgb(0,0,0)"><br><=
/span><span style=3D"color:rgb(102,102,0)">{</span><span style=3D"color:rgb=
(0,0,0)"><br>=C2=A0 =C2=A0 </span><span style=3D"color:rgb(0,0,136)">static=
_assert</span><span style=3D"color:rgb(102,102,0)">(</span><span style=3D"c=
olor:rgb(0,0,136)">sizeof</span><span style=3D"color:rgb(102,102,0)">(</spa=
n><span style=3D"color:rgb(102,0,102)">ResultType</span><span style=3D"colo=
r:rgb(102,102,0)">)</span><span style=3D"color:rgb(0,0,0)"> </span><span st=
yle=3D"color:rgb(102,102,0)">></span><span style=3D"color:rgb(0,0,0)"> <=
/span><span style=3D"color:rgb(0,0,136)">sizeof</span><span style=3D"color:=
rgb(102,102,0)">(</span><span style=3D"color:rgb(0,0,136)">decltype</span><=
span style=3D"color:rgb(102,102,0)">(</span><span style=3D"color:rgb(0,0,0)=
">s</span><span style=3D"color:rgb(102,102,0)">))</span><span style=3D"colo=
r:rgb(0,0,0)"> </span><span style=3D"color:rgb(102,102,0)">||</span><span s=
tyle=3D"color:rgb(0,0,0)"> std</span><span style=3D"color:rgb(102,102,0)">:=
:</span><span style=3D"color:rgb(0,0,0)">is_same_v</span><span style=3D"col=
or:rgb(102,102,0)"><</span><span style=3D"color:rgb(102,0,102)">ResultTy=
pe</span><span style=3D"color:rgb(102,102,0)">,</span><span style=3D"color:=
rgb(0,0,0)"> std</span><span style=3D"color:rgb(102,102,0)">::</span><span =
style=3D"color:rgb(0,0,0)">make_unsigned_t</span><span style=3D"color:rgb(1=
02,102,0)"><</span><span style=3D"color:rgb(102,0,102)">ValueType</span>=
<span style=3D"color:rgb(102,102,0)">>>,</span><span style=3D"color:r=
gb(0,0,0)"> </span><span style=3D"color:rgb(0,136,0)">"The result type=
must be able to store the positive value!"</span><span style=3D"color=
:rgb(102,102,0)">);</span><span style=3D"color:rgb(0,0,0)"><br>=C2=A0 =C2=
=A0 </span><span style=3D"color:rgb(102,0,102)">ResultType</span><span styl=
e=3D"color:rgb(0,0,0)"> absVal</span><span style=3D"color:rgb(102,102,0)">;=
</span><span style=3D"color:rgb(0,0,0)"><br>=C2=A0 =C2=A0 <br>=C2=A0 =C2=A0=
</span><span style=3D"color:rgb(0,0,136)">if</span><span style=3D"color:rg=
b(102,102,0)">(</span><span style=3D"color:rgb(0,0,0)"> s </span><span styl=
e=3D"color:rgb(102,102,0)">>=3D</span><span style=3D"color:rgb(0,0,0)"> =
</span><span style=3D"color:rgb(0,102,102)">0</span><span style=3D"color:rg=
b(0,0,0)"> </span><span style=3D"color:rgb(102,102,0)">)</span><span style=
=3D"color:rgb(0,0,0)"><br>=C2=A0 =C2=A0 </span><span style=3D"color:rgb(102=
,102,0)">{</span><span style=3D"color:rgb(0,0,0)"><br>=C2=A0 =C2=A0 =C2=A0 =
=C2=A0 </span><span style=3D"color:rgb(0,0,136)">return</span><span style=
=3D"color:rgb(0,0,0)"> </span><span style=3D"color:rgb(0,0,136)">static_cas=
t</span><span style=3D"color:rgb(102,102,0)"><</span><span style=3D"colo=
r:rgb(102,0,102)">ResultType</span><span style=3D"color:rgb(102,102,0)">>=
;(</span><span style=3D"color:rgb(0,0,0)">s</span><span style=3D"color:rgb(=
102,102,0)">);</span><span style=3D"color:rgb(0,0,0)"><br>=C2=A0 =C2=A0 </s=
pan><span style=3D"color:rgb(102,102,0)">}</span><span style=3D"color:rgb(0=
,0,0)"><br>=C2=A0 =C2=A0 </span><span style=3D"color:rgb(0,0,136)">else</sp=
an><span style=3D"color:rgb(0,0,0)"><br>=C2=A0 =C2=A0 </span><span style=3D=
"color:rgb(102,102,0)">{</span><span style=3D"color:rgb(0,0,0)"><br>=C2=A0 =
=C2=A0 =C2=A0 =C2=A0 absVal </span><span style=3D"color:rgb(102,102,0)">=3D=
</span><span style=3D"color:rgb(0,0,0)"> </span><span style=3D"color:rgb(10=
2,102,0)">~</span><span style=3D"color:rgb(0,0,0)">s</span><span style=3D"c=
olor:rgb(102,102,0)">;</span><span style=3D"color:rgb(0,0,0)"><br>=C2=A0 =
=C2=A0 =C2=A0 =C2=A0 absVal</span><span style=3D"color:rgb(102,102,0)">++;<=
/span><span style=3D"color:rgb(0,0,0)"><br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 </sp=
an><span style=3D"color:rgb(0,0,136)">return</span><span style=3D"color:rgb=
(0,0,0)"> absVal</span><span style=3D"color:rgb(102,102,0)">;</span><span s=
tyle=3D"color:rgb(0,0,0)"><br>=C2=A0 =C2=A0 </span><span style=3D"color:rgb=
(102,102,0)">}</span><span style=3D"color:rgb(0,0,0)"><br></span><span styl=
e=3D"color:rgb(102,102,0)">}</span><span style=3D"color:rgb(0,0,0)"><br><br=
></span><span style=3D"color:rgb(0,0,136)">int</span><span style=3D"color:r=
gb(0,0,0)"> main</span><span style=3D"color:rgb(102,102,0)">(</span><span s=
tyle=3D"color:rgb(0,0,136)">void</span><span style=3D"color:rgb(102,102,0)"=
>)</span><span style=3D"color:rgb(0,0,0)"><br></span><span style=3D"color:r=
gb(102,102,0)">{</span><span style=3D"color:rgb(0,0,0)"><br>=C2=A0 =C2=A0 s=
td</span><span style=3D"color:rgb(102,102,0)">::</span><span style=3D"color=
:rgb(0,0,0)">cout </span><span style=3D"color:rgb(102,102,0)"><<</spa=
n><span style=3D"color:rgb(0,0,0)"> abs</span><span style=3D"color:rgb(102,=
102,0)"><</span><span style=3D"color:rgb(0,0,0)">std</span><span style=
=3D"color:rgb(102,102,0)">::</span><span style=3D"color:rgb(0,0,0)">uint8_t=
</span><span style=3D"color:rgb(102,102,0)">>(</span><span style=3D"colo=
r:rgb(0,0,0)">std</span><span style=3D"color:rgb(102,102,0)">::</span><span=
style=3D"color:rgb(0,0,0)">numeric_limits</span><span style=3D"color:rgb(1=
02,102,0)"><</span><span style=3D"color:rgb(0,0,0)">std</span><span styl=
e=3D"color:rgb(102,102,0)">::</span><span style=3D"color:rgb(0,0,0)">int8_t=
</span><span style=3D"color:rgb(102,102,0)">>::</span><span style=3D"col=
or:rgb(0,0,0)">min</span><span style=3D"color:rgb(102,102,0)">())</span><sp=
an style=3D"color:rgb(0,0,0)"> </span><span style=3D"color:rgb(102,102,0)">=
<<</span><span style=3D"color:rgb(0,0,0)"> </span><span style=3D"colo=
r:rgb(0,136,0)">'\n'</span><span style=3D"color:rgb(102,102,0)">;</=
span><span style=3D"color:rgb(0,0,0)"><br>=C2=A0 =C2=A0 std</span><span sty=
le=3D"color:rgb(102,102,0)">::</span><span style=3D"color:rgb(0,0,0)">cout =
</span><span style=3D"color:rgb(102,102,0)"><<</span><span style=3D"c=
olor:rgb(0,0,0)"> abs</span><span style=3D"color:rgb(102,102,0)"><</span=
><span style=3D"color:rgb(0,0,0)">std</span><span style=3D"color:rgb(102,10=
2,0)">::</span><span style=3D"color:rgb(0,0,0)">int32_t</span><span style=
=3D"color:rgb(102,102,0)">>(</span><span style=3D"color:rgb(0,0,0)">std<=
/span><span style=3D"color:rgb(102,102,0)">::</span><span style=3D"color:rg=
b(0,0,0)">numeric_limits</span><span style=3D"color:rgb(102,102,0)"><</s=
pan><span style=3D"color:rgb(0,0,0)">std</span><span style=3D"color:rgb(102=
,102,0)">::</span><span style=3D"color:rgb(0,0,0)">int16_t</span><span styl=
e=3D"color:rgb(102,102,0)">>::</span><span style=3D"color:rgb(0,0,0)">mi=
n</span><span style=3D"color:rgb(102,102,0)">())</span><span style=3D"color=
:rgb(0,0,0)"> </span><span style=3D"color:rgb(102,102,0)"><<</span><s=
pan style=3D"color:rgb(0,0,0)"> </span><span style=3D"color:rgb(0,136,0)">&=
#39;\n'</span><span style=3D"color:rgb(102,102,0)">;</span><span style=
=3D"color:rgb(0,0,0)"><br>=C2=A0 =C2=A0 </span><span style=3D"color:rgb(136=
,0,0)">//std::cout << abs<std::uint8_t>(std::numeric_limits<=
std::int64_t>::min()) << '\n';</span><span style=3D"color:=
rgb(0,0,0)"><br>=C2=A0 =C2=A0 </span><span style=3D"color:rgb(0,0,136)">ret=
urn</span><span style=3D"color:rgb(0,0,0)"> </span><span style=3D"color:rgb=
(0,102,102)">0</span><span style=3D"color:rgb(102,102,0)">;</span><span sty=
le=3D"color:rgb(0,0,0)"><br></span><span style=3D"color:rgb(102,102,0)">}</=
span></div></code></div><br>it can be named abs, because as the result type=
must be specified it is different from the existing abs overloads and thus=
does not need a new name.</div><div>So I agree to abs<unsigned>();</=
div><div>And if you do not care about safety you could even leave out the s=
tatic_assert. We could also make the return type be any arithmetic type.<br=
></div><div>In this case floor<int>(), ceil<int>() and round<=
;int>() and maybe other similar functions could also be added.</div><br>=
Am Sonntag, 14. Oktober 2018 17:18:09 UTC+2 schrieb John McFarlane:<blockqu=
ote class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;border-left:1px=
solid rgb(204,204,204);padding-left:1ex"><br><br><div class=3D"gmail_quote=
"><div dir=3D"ltr">On Sun, Oct 14, 2018, 03:31 Bengt Gustafsson <<a rel=
=3D"nofollow noreferrer">bengt.gu...@beamways.com</a>> wrote:<br></div><=
blockquote class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;border-l=
eft:1px solid rgb(204,204,204);padding-left:1ex"><div dir=3D"ltr">For me th=
e motivation for uabs() would more be to avoid unsigned/signed mismatch err=
ors by getting the result as an unsigned (which we know it conceptually is =
after abs).</div></blockquote></div><div><br></div><div>I consider a number=
being positive as different to a number being unsigned. I'd rather tho=
se two things (conversion and absolute value)=C2=A0remained separate concer=
ns.<br></div><div class=3D"gmail_quote"><blockquote class=3D"gmail_quote" s=
tyle=3D"margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);pad=
ding-left:1ex"><div dir=3D"ltr"><div><br></div><div>This is very similar to=
the annoyance of floor, round and ceil returning floats which we know have=
integer values, forcing us to write int(round(x)) when using this with oth=
er ints to avoid compiler warnings. I think I saw something about new floor=
<int>() and similar functions being proposed, so why not uabs?</div><=
/div></blockquote></div><div><br></div><div>I'm less adverse to abs<=
unsigned>() because the two concerns are more apparent but only slightly=
..=C2=A0</div><div><br></div><div>If you're converting a float to an int=
, a necessary detail is how to approximate fractional values. So a convert&=
lt;int, floor_tag>() starts to become palatable. p0105 proposed somethin=
g similar. I'm not familiar with the floor function you mention.</div><=
div class=3D"gmail_quote"><blockquote class=3D"gmail_quote" style=3D"margin=
:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"=
><div dir=3D"ltr"><div><br></div></div>
<p></p>
-- <br>
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" group.<br>
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a rel=3D"nofollow noreferrer">std-proposal...@isocpp.org</a>.<br>
To post to this group, send email to <a rel=3D"nofollow noreferrer">std-pr.=
...@isocpp.org</a>.<br>
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/3a5821ae-c386-46b4-8e73-4bff8764679c%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter" rel=3D"nofollow no=
referrer" target=3D"_blank">https://groups.google.com/a/isocpp.org/d/msgid/=
std-proposals/3a5821ae-c386-46b4-8e73-4bff8764679c%40isocpp.org</a>.<br>
</blockquote></div>
</blockquote></div></blockquote></div>
<p></p>
-- <br>
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" group.<br>
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org" rel=3D"nore=
ferrer" target=3D"_blank">std-proposals+unsubscribe@isocpp.org</a>.<br>
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org" rel=3D"noreferrer" target=3D"_blank">std-proposals@isocpp.org</a>.<br=
>
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/9d74ddbb-cc68-412a-ae6b-4664660ff12c%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter" rel=3D"noreferrer"=
target=3D"_blank">https://groups.google.com/a/isocpp.org/d/msgid/std-propo=
sals/9d74ddbb-cc68-412a-ae6b-4664660ff12c%40isocpp.org</a>.<br>
</blockquote></div>
</blockquote></div></div></div>
<p></p>
-- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/CACeCrotMrChHSoswwYpxqRfd9OR2Fsfoz05R=
wOm3w%2BaEBqEGQw%40mail.gmail.com?utm_medium=3Demail&utm_source=3Dfooter">h=
ttps://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CACeCrotMrChHSo=
swwYpxqRfd9OR2Fsfoz05RwOm3w%2BaEBqEGQw%40mail.gmail.com</a>.<br />
--0000000000009548b10578b0e0e0--
.
Author: Jens Maurer <Jens.Maurer@gmx.net>
Date: Sun, 21 Oct 2018 09:05:09 +0200
Raw View
On 10/21/2018 12:52 AM, JF Bastien wrote:
>=20
>=20
> On Sat, Oct 20, 2018 at 1:00 PM Bryce Adelstein Lelbach aka wash <brycele=
lbach@gmail.com <mailto:brycelelbach@gmail.com>> wrote:
>=20
> JF? (See question below)
>=20
> On Sat, Oct 20, 2018, 7:27 AM <burningorca@gmail.com <mailto:burningo=
rca@gmail.com>> wrote:
>=20
> I recently read that their is a paper suggesting integers always =
being twos complement should be standardised. If this is the case, would th=
e call to the existing std::abs(std::numeric_limits<SignedType>::min()) bec=
ome well defined behaviour?
>=20
>=20
> http://wg21.link/p0907
>=20
> /Status-quo/ If a signed operation would naturally produce a value th=
at is not within the range of the result type, the behavior is undefined. T=
he author had hoped to make this well-defined as wrapping (the operations p=
roduce the same value bits as for the corresponding unsigned type), but WG2=
1 had strong resistance against this.
In particular, the wording surrounding std::abs in the C++ standard is unch=
anged
"Effects: The abs functions have the semantics specified in the C standard =
library
for the functions abs, labs, llabs, fabsf, fabs, and fabsl."
and C says
"The abs, labs, and llabs functions compute the absolute value of an intege=
r j.
If the result cannot be represented, the behavior is undefined."
Jens
--=20
You received this message because you are subscribed to the Google Groups "=
ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp=
..org/d/msgid/std-proposals/5BCC2525.90805%40gmx.net.
.