A strange div bug on Linux x86_64, (both dmd & ldc2): long -5000 / size_t 2 = 9223372036854773308

mw mingwu at gmail.com
Thu Aug 13 18:51:09 UTC 2020


On Thursday, 13 August 2020 at 18:40:40 UTC, matheus wrote:
> On Thursday, 13 August 2020 at 13:33:19 UTC, bachmeier wrote:
>> ...
>> The source of wrong behavior is vec.length having type ulong. 
>> It would be very unusual for someone to even think about that.
>
> May I ask what type should it be?

Signed (size_t, the length of the machine's address space)

Just as in Java (as an improvement of C++):

https://stackoverflow.com/questions/211311/what-is-the-data-type-for-length-property-for-java-arrays

It is an int. See the Java Language Specification, section 10.7.


Initially, Java don't have unsigned integer type; it's added as 
late as Java 8, and when it's added, they also added the extra 
methods to properly handle them:

https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

"""
int: By default, the int data type is a 32-bit signed two's 
complement integer, which has a minimum value of -231 and a 
maximum value of 231-1. In Java SE 8 and later, you can use the 
int data type to represent an unsigned 32-bit integer, which has 
a minimum value of 0 and a maximum value of 232-1. Use the 
Integer class to use int data type as an unsigned integer. See 
the section The Number Classes for more information. Static 
methods like compareUnsigned, divideUnsigned etc have been added 
to the Integer class to support the arithmetic operations for 
unsigned integers.
"""

In contrast, D does the potential harmful conversion *silently*.



More information about the Digitalmars-d mailing list