Is this normal or a bug ?

Abdulhaq alynch4048 at gmail.com
Mon Jul 24 17:27:26 UTC 2023


On Monday, 24 July 2023 at 13:25:17 UTC, cryptonian wrote:
> On Monday, 3 July 2023 at 08:24:22 UTC, Mehdi wrote:
>> Hello, d community! I am currently exploring the possibilities 
>> of interoperability between C++ and D. While conducting some 
>> tests, I came across an interesting case. Here is the C++ code 
>> snippet:
>>
>> ```cpp
>> float float_test(float x, float y){
>>   return x+y;
>> }
>> ```
>>
>> And here is the corresponding D code:
>>
>> ```d
>> extern (C++) float float_test(float x, float y);
>> {
>>   assert(10.1 + 20.6 == float_test(10.1, 20.6));
>> }
>> ```
>>
>> Unfortunately, the assert statement fails in this case. I'm 
>> wondering if this discrepancy is due to the different float 
>> representations, a bug, or if I'm doing something wrong. Any 
>> insights would be greatly appreciated!
>
> The difference in the assert statement could be because of the 
> different float representations used in C++ and D. C++ and D 
> might employ different floating-point models, leading to minor 
> precision differences in arithmetic operations. To resolve 
> this, consider using data types or libraries that ensure 
> consistent float representations across both languages.
> I learned this during my probation time in the organization, a 
> <a 
> href=“https://thinkpalm.com/technologies/artificial-inteligence/”>machine learning and AI services provider</a>. Such programs are very rare I guess.

I once had an "issue" in python where the same calculation, when 
run multiple times, would produce floats of differing exact 
values in the 17th d.p. On investigation I learnt it was due (at 
that time) to the processor sometimes using a particular 80bit 
register, and sometimes not IIRC.


More information about the Digitalmars-d mailing list