Jump to page: 1 2
Thread overview
Bug? 0 is less than -10
Oct 06, 2015
tcak
Oct 06, 2015
Adam D. Ruppe
Oct 06, 2015
Laeeth Isharc
Oct 07, 2015
Laeeth Isharc
Oct 07, 2015
Andrea Fontana
Oct 07, 2015
Laeeth Isharc
Oct 08, 2015
Laeeth Isharc
Oct 06, 2015
anonymous
Oct 06, 2015
Meta
Oct 07, 2015
Marc Schütz
Oct 09, 2015
Ozan
October 06, 2015
Maybe I am just too stressed out to see the problem.

[code]
import std.stdio;

void main(){
	size_t dec = 0;

	writeln( dec, " ", (dec <= -10), " ", (dec >= 10), " ", ((dec <= -10) || (dec >= 10)) );
}
[/code]

[output]
0 true false true
[/output]

How is it generating "true" for (dec <= -10) ? Is there a special casting or something?

DMD 2.068.2, Ubuntu 64-bit
October 06, 2015
On Tuesday, 6 October 2015 at 14:46:56 UTC, tcak wrote:
> void main(){
> 	size_t dec = 0;

> How is it generating "true" for (dec <= -10) ? Is there a special casting or something?

size_t is unsigned, so the -10 is cast to unsigned too for the comparison which yields some huge number.

Comparing signed to unsigned is almost always a mistake... but one D inherited from C.

This is a reason why I prefer to use int instead of size_t where I can but that might require casts and truncation too.
October 06, 2015
On Tuesday, 6 October 2015 at 14:46:56 UTC, tcak wrote:
> Maybe I am just too stressed out to see the problem.
>
> [code]
> import std.stdio;
>
> void main(){
> 	size_t dec = 0;
>
> 	writeln( dec, " ", (dec <= -10), " ", (dec >= 10), " ", ((dec <= -10) || (dec >= 10)) );
> }
> [/code]
>
> [output]
> 0 true false true
> [/output]
>
> How is it generating "true" for (dec <= -10) ? Is there a special casting or something?
>
> DMD 2.068.2, Ubuntu 64-bit

dec is a size_t. size_t is unsigned. -10 is cast to unsigned for the comparison, resulting in some huge value.
October 06, 2015
On Tuesday, 6 October 2015 at 14:46:56 UTC, tcak wrote:
> Maybe I am just too stressed out to see the problem.
>
> [code]
> import std.stdio;
>
> void main(){
> 	size_t dec = 0;
>
> 	writeln( dec, " ", (dec <= -10), " ", (dec >= 10), " ", ((dec <= -10) || (dec >= 10)) );
> }
> [/code]
>
> [output]
> 0 true false true
> [/output]
>
> How is it generating "true" for (dec <= -10) ? Is there a special casting or something?
>
> DMD 2.068.2, Ubuntu 64-bit

If you need to mix signed and unsigned values but want to avoid this issue, you an use the comparison functions from std.functional.

http://dlang.org/phobos/std_functional.html#.lessThan
October 06, 2015
On Tuesday, 6 October 2015 at 14:55:23 UTC, Adam D. Ruppe wrote:
> On Tuesday, 6 October 2015 at 14:46:56 UTC, tcak wrote:
>> void main(){
>> 	size_t dec = 0;
>
>> How is it generating "true" for (dec <= -10) ? Is there a special casting or something?
>
> size_t is unsigned, so the -10 is cast to unsigned too for the comparison which yields some huge number.
>
> Comparing signed to unsigned is almost always a mistake... but one D inherited from C.
>
> This is a reason why I prefer to use int instead of size_t where I can but that might require casts and truncation too.

could we have ssize_t defined in phobos somewhere so your code ends up being portable ;) (It's trivial to do, obviously).
October 07, 2015
On 10/6/15 7:21 PM, Laeeth Isharc wrote:
> could we have ssize_t defined in phobos somewhere so your code ends up
> being portable ;) (It's trivial to do, obviously).

ptrdiff_t

-Steve
October 07, 2015
On Wednesday, 7 October 2015 at 02:53:32 UTC, Steven Schveighoffer wrote:
> On 10/6/15 7:21 PM, Laeeth Isharc wrote:
>> could we have ssize_t defined in phobos somewhere so your code ends up
>> being portable ;) (It's trivial to do, obviously).
>
> ptrdiff_t
>
> -Steve

It seems unnatural to use such a name when the variable has nothing to do with pointers - it doesn't contribute to the readability.  Yes, it's trivial, but small things cumulatively matter.  Adam tends to use int and when that gets mixed up with an auto size_t (eg via length) then his code doesn't compile on 64 bit.  And if it happens with his code, you can imagine this isn't a problem that inexperienced users never encounter.
October 07, 2015
On Wednesday, 7 October 2015 at 05:27:12 UTC, Laeeth Isharc wrote:
> On Wednesday, 7 October 2015 at 02:53:32 UTC, Steven Schveighoffer wrote:
>> On 10/6/15 7:21 PM, Laeeth Isharc wrote:
>>> could we have ssize_t defined in phobos somewhere so your code ends up
>>> being portable ;) (It's trivial to do, obviously).
>>
>> ptrdiff_t
>>
>> -Steve
>
> It seems unnatural to use such a name when the variable has nothing to do with pointers - it doesn't contribute to the readability.  Yes, it's trivial, but small things cumulatively matter.  Adam tends to use int and when that gets mixed up with an auto size_t (eg via length) then his code doesn't compile on 64 bit.  And if it happens with his code, you can imagine this isn't a problem that inexperienced users never encounter.

IMO it seems unnatural to use size_t with a signed/negative value too.

October 07, 2015
On Wednesday, 7 October 2015 at 07:38:44 UTC, Andrea Fontana wrote:
> On Wednesday, 7 October 2015 at 05:27:12 UTC, Laeeth Isharc wrote:
>> On Wednesday, 7 October 2015 at 02:53:32 UTC, Steven Schveighoffer wrote:
>>> On 10/6/15 7:21 PM, Laeeth Isharc wrote:
>>>> could we have ssize_t defined in phobos somewhere so your code ends up
>>>> being portable ;) (It's trivial to do, obviously).
>>>
>>> ptrdiff_t
>>>
>>> -Steve
>>
>> It seems unnatural to use such a name when the variable has nothing to do with pointers - it doesn't contribute to the readability.  Yes, it's trivial, but small things cumulatively matter.  Adam tends to use int and when that gets mixed up with an auto size_t (eg via length) then his code doesn't compile on 64 bit.  And if it happens with his code, you can imagine this isn't a problem that inexperienced users never encounter.
>
> IMO it seems unnatural to use size_t with a signed/negative value too.
Indeed.  But you end up with an inferred size_t in a foreach and using length with auto, so it makes sense to have something nicer that you can specify that won't make your code look strange.  Doesn't bother me since I define ssize_t, but it might make D easier to read for others.  When you see ptrdiff_t with no pointer in sight it's not very clear unless you are familiar with C idioms (and I never remember seeing this back when I learnt C - some modern thing).

October 07, 2015
On Tuesday, 6 October 2015 at 14:46:56 UTC, tcak wrote:
> Maybe I am just too stressed out to see the problem.
>
> [code]
> import std.stdio;
>
> void main(){
> 	size_t dec = 0;
>
> 	writeln( dec, " ", (dec <= -10), " ", (dec >= 10), " ", ((dec <= -10) || (dec >= 10)) );
> }
> [/code]
>
> [output]
> 0 true false true
> [/output]
>
> How is it generating "true" for (dec <= -10) ? Is there a special casting or something?
>
> DMD 2.068.2, Ubuntu 64-bit

Lionello Lunesu posted a PR that should fix this:
https://github.com/D-Programming-Language/dmd/pull/1913
See also the discussion in the linked bug report.

Unfortunately it seems it's been forgotten since then...
« First   ‹ Prev
1 2