Jump to page: 1 2 3
Thread overview
abs and minimum values
Oct 28, 2021
kyle
Oct 28, 2021
kyle
Oct 29, 2021
Dom DiSc
Oct 29, 2021
Ali Çehreli
Oct 31, 2021
Dom DiSc
Oct 31, 2021
Siarhei Siamashka
Oct 31, 2021
Imperatorn
Oct 31, 2021
Elronnd
Oct 31, 2021
Ali Çehreli
Oct 31, 2021
Imperatorn
Oct 29, 2021
Imperatorn
Oct 29, 2021
Dom DiSc
Oct 29, 2021
Imperatorn
Oct 29, 2021
Kagamin
Oct 29, 2021
Imperatorn
Oct 29, 2021
Bruce Carneal
Oct 29, 2021
Salih Dincer
Oct 29, 2021
kyle
Oct 31, 2021
Siarhei Siamashka
October 28, 2021
void main()
{
    import std.math : abs, sgn;

    alias n_type = short; //or int, long, byte, whatever

    assert(n_type.min == abs(n_type.min));
    assert(sgn(abs(n_type.min)) == -1);
}

I stumbled into this fun today. I understand why abs yields a negative value here with overflow and no promotion. I just want to know if it should. Should abs ever return a negative number? Thanks.

October 28, 2021

On Thursday, 28 October 2021 at 21:23:15 UTC, kyle wrote:

>
void main()
{
    import std.math : abs, sgn;

    alias n_type = short; //or int, long, byte, whatever

    assert(n_type.min == abs(n_type.min));
    assert(sgn(abs(n_type.min)) == -1);
}

I stumbled into this fun today. I understand why abs yields a negative value here with overflow and no promotion. I just want to know if it should. Should abs ever return a negative number? Thanks.

Okay I checked the phobos docs and it does say "Limitations
Does not work correctly for signed intergal types and value Num.min." Should have looked there first, I know. Still seems pretty silly.

October 29, 2021

On Thursday, 28 October 2021 at 21:26:04 UTC, kyle wrote:

>

Okay I checked the phobos docs and it does say "Limitations
Does not work correctly for signed intergal types and value Num.min." Should have looked there first, I know. Still seems pretty silly.

I recommend to implement your own abs function this way (was not accepted for phobos, as it now does NOT return the same type as the argument, which was considered a "breaking change" :-( ):

/// get the absolute value of x as unsigned type. always succeeds, even for T.min
Unsigned!T abs(T)(const(T) x) if(isIntegral!T)
{
   static if(isSigned!T) if(x < 0) return cast(Unsigned!T)-x;
   return x;
}
October 29, 2021

On Thursday, 28 October 2021 at 21:23:15 UTC, kyle wrote:

>
void main()
{
    import std.math : abs, sgn;

    alias n_type = short; //or int, long, byte, whatever

    assert(n_type.min == abs(n_type.min));
    assert(sgn(abs(n_type.min)) == -1);
}

I stumbled into this fun today. I understand why abs yields a negative value here with overflow and no promotion. I just want to know if it should. Should abs ever return a negative number? Thanks.

Depends on how you view it. Imo abs should never be able to return a negative value since it should be the distance/length from 0 and alternaticely root of x^2 .

October 29, 2021

On Friday, 29 October 2021 at 08:33:07 UTC, Imperatorn wrote:

>

Imo abs should never be able to return a negative value

Yes, but phobos defines it to return a signed type, so theoretical it can return negative values. And they won't change that.

I really think it should return an unsigned type, but that's a "breaking change".
Bullshit policy!

October 29, 2021

On Friday, 29 October 2021 at 09:35:09 UTC, Dom DiSc wrote:

>

On Friday, 29 October 2021 at 08:33:07 UTC, Imperatorn wrote:

>

Imo abs should never be able to return a negative value

Yes, but phobos defines it to return a signed type, so theoretical it can return negative values. And they won't change that.

I really think it should return an unsigned type, but that's a "breaking change".
Bullshit policy!

Eeh... Why does it return unsigned in phobos? Is it just to stay compatible with C? 🤔

October 29, 2021
On 10/29/21 1:05 AM, Dom DiSc wrote:

> I recommend to implement your own abs function this way (was not
> accepted for phobos, as it now does NOT return the same type as the
> argument, which was considered a "breaking change" :-( ):

Combined with automatic type conversions we got from C, it can cause unexpected results as well. One might expect the following program to print -1 when that definition of abs() is used in an expression:

import std.traits;
import std.stdio;

/// get the absolute value of x as unsigned type. always succeeds, even for T.min
Unsigned!T abs(T)(const(T) x) if(isIntegral!T)
{
   static if(isSigned!T) if(x < 0) return cast(Unsigned!T)-x;
   return x;
}

void main() {
  int a = -5;
  int b = -4;
  writeln(a + abs(b)); // -5 + 4 == -1? (No!)
}

The program prints uint.max.

Ali

October 29, 2021

Unsigned integers aren't numbers.
assert(-abs(1)<0);

October 29, 2021

On Friday, 29 October 2021 at 14:23:49 UTC, Kagamin wrote:

>

Unsigned integers aren't numbers.
assert(-abs(1)<0);

That's what I mean. The mapping between number classes and data types are too vague.

October 29, 2021

On Friday, 29 October 2021 at 14:23:49 UTC, Kagamin wrote:

>

Unsigned integers aren't numbers.
assert(-abs(1)<0);

Unsigneds approximate whole numbers of course (truncated on one side). Likewise signeds approximate integers (across a restricted interval). As always, we need to be careful with approximations.

« First   ‹ Prev
1 2 3