Project

General

Profile

Actions

Defect #755

closed

-0.0 does not match C/C++ bits conversion

Added by Runar Tenfjord about 1 month ago. Updated about 1 month ago.

Status:
Closed
Priority:
Normal
Category:
Oberon Front-End
Target version:
-
% Done:

100%


Description

Consider the following code:

MODULE Test;

IMPORT SYSTEM;

PROCEDURE ReinterpretBits(val : REAL32): UNSIGNED32;
BEGIN RETURN SYSTEM.VAL(UNSIGNED32, val)
END ReinterpretBits;

BEGIN
    TRACE(ReinterpretBits(-0.0)); (* Does not match C *)
    TRACE(ReinterpretBits(+0.0));
    TRACE(ReinterpretBits(-1.0));
    TRACE(ReinterpretBits(+1.0));
    TRACE(ReinterpretBits(-2.0));
    TRACE(ReinterpretBits(+2.0));
END Test.

which outputs:

Test.mod:13:26: note: 'ReinterpretBits (-0.0)' = 0
Test.mod:14:26: note: 'ReinterpretBits (+0.0)' = 0
Test.mod:15:26: note: 'ReinterpretBits (-1.0)' = 3212836864
Test.mod:16:26: note: 'ReinterpretBits (+1.0)' = 1065353216
Test.mod:17:26: note: 'ReinterpretBits (-2.0)' = 3221225472
Test.mod:18:26: note: 'ReinterpretBits (+2.0)' = 1073741824

Corresponding C code:

#include <stdio.h>
#include <stdint.h>

static  uint32_t  ReinterpretBits(float source)
{
    union {
        float f;
        uint32_t u;
    } fu;
    fu.f = source;
    return fu.u;
}

int main() {
  printf("%u\n", ReinterpretBits(-0.0));
  printf("%u\n", ReinterpretBits(+0.0));
  printf("%u\n", ReinterpretBits(-1.0));
  printf("%u\n", ReinterpretBits(+1.0));
  printf("%u\n", ReinterpretBits(-2.0));
  printf("%u\n", ReinterpretBits(+2.0));
}

which outputs:

2147483648
0
3212836864
1065353216
3221225472
1073741824

It seems that the value -0.0 ignores the - sign which is significant for REAL type.


Files

negative_zero.patch (1.75 KB) negative_zero.patch Florian Negele, 29 June 2026 08:41
Actions

Also available in: Atom PDF