float.sizeof should be 4

Steven Schveighoffer schveiguy at gmail.com
Sun Sep 22 17:52:10 UTC 2024


On Sunday, 22 September 2024 at 16:45:23 UTC, Johan wrote:
> On Sunday, 22 September 2024 at 14:59:36 UTC, Timon Gehr wrote:
>> On 9/22/24 14:59, Steven Schveighoffer wrote:
>>> Not `4LU`.
>>> 
>>> For the billionth time while porting raylib to D, I'm having 
>>> to replace `n*float.sizeof` with `n*int(float.sizeof)` 
>>> because otherwise, it doesn't fit into the `int` I'm 
>>> assigning it to.
>>> 
>>> Why isn't it just 4? 4 will convert to any width you want. It 
>>> does not poison integer multiplications or additions.
>>> 
>>> The chances that `T.sizeof > int.max` is near zero. And even 
>>> if it did, making it the same as a number literal would just 
>>> work -- it would become `size_t` if it was too big.
>>> 
>>
>> Well, the point of contention is not that `float.sizeof > 
>> int.max`. Rather, `n*float.sizeof` could be bigger than 
>> `int.max`.

This is true for any integer calculation. Though, it is true that 
this would be painful if you'd have to increase the precision 
explicitly, and a failure to do so would be a silent error. So I 
have to withdraw this.

The correct solution is likely to just write a function to do 
this correctly. Ugh.

> Is that true?
> `n*int(float.sizeof)` could also be bigger than `int.max`, but 
> Steven says it works for his case.  (@Steven: best to show the 
> full case).

See all the modifications here: 
https://github.com/schveiguy/draylib/commit/b665f75c453afa6ab8e4f8108fe1bd296f72d53e#diff-10364ecc46979709c69d6caaeb639fedf2ffa2db80d4d61a7172b7a8a374dd12

(you have to tell github to load the diff).

-Steve


More information about the Digitalmars-d mailing list