[OT] Rounding v Truncation, was: Re: Platform Support vs.

From: Simon Clubley (clubley_at_remove_me.eisner.decus.org-Earth.UFP)
Date: 07/25/05


Date: 25 Jul 2005 07:52:36 -0500

In article <11e6169idjh2n3a@corp.supernews.com>, Dave Froble <davef@tsoft-inc.com> writes:
>
> When I had a problem with Visual Basic she defended VB and wouldn't
> consider it wrong. So I'll ask, actually maybe take a poll. If a real
> value is placed into an integer variable, how many people would expect
> the fraction to be truncated? I'd expect most would. I did. VAX/DEC
> BASIC truncates. Well, VB rounds, and fractional parts of .5 and
> greater round up. Sure screwed with the logic from the converted VAX
> Basic applications. Sure screwed with my mind.
>

I would regard rounding to be the correct thing to do from a maths viewpoint.

However, I created two examples, one in Ada and another in C which
exhibit different behaviour. Ada rounds and C truncates:

[simon@?????? ft]# cat float_test.adb
with Text_IO; use Text_IO;

procedure float_test is

        Target: Integer;
        Source: Float;

begin
        Source := 3.4;
        Target := Integer(Source);
        Put_Line("Source = " & Source'Img & ", Target = " & Target'Img);

        Source := 3.5;
        Target := Integer(Source);
        Put_Line("Source = " & Source'Img & ", Target = " & Target'Img);

        Source := 3.6;
        Target := Integer(Source);
        Put_Line("Source = " & Source'Img & ", Target = " & Target'Img);
end float_test;
[simon@?????? ft]# ./float_test
Source = 3.40000E+00, Target = 3
Source = 3.50000E+00, Target = 4
Source = 3.60000E+00, Target = 4
[simon@?????? ft]# cat float_test.c
#include <stdio.h>

int main()
        {
        int target;
        float source;

        source = 3.4;
        target = source;
        printf("Source = %f, target = %d\n", source, target);

        source = 3.5;
        target = source;
        printf("Source = %f, target = %d\n", source, target);

        source = 3.6;
        target = source;
        printf("Source = %f, target = %d\n", source, target);

        return(0);
        }
[simon@?????? ft]# ./float_test_c
Source = 3.400000, target = 3
Source = 3.500000, target = 3
Source = 3.600000, target = 3
[simon@?????? ft]#

The Ada behaviour is specified in the Ada Language Reference Manual (section
4.6, line 33), see: http://www.adapower.com/rm95/RM-4-6.html :

        If the target type is an integer type and the operand type is real,
        the result is rounded to the nearest integer (away from zero if
        exactly halfway between two integers).

Simon.

-- 
Simon Clubley, clubley@remove_me.eisner.decus.org-Earth.UFP       
Microsoft: The Standard Oil Company of the 21st century


Relevant Pages

  • Re: [OT] Rounding v Truncation, was: Re: Platform Support vs.
    ... Ada rounds and C truncates: ... FREJA> create rnd_trnc.pli ...
    (comp.os.vms)
  • Re: [OT] Rounding v Truncation, was: Re: Platform Support vs.
    ... > I would regard rounding to be the correct thing to do from a maths viewpoint. ... memories of integer operation rules is that any fractional part is ... Ada rounds and C truncates: ...
    (comp.os.vms)
  • Ouch! Inconsistent TEXT("hh:mm") rounding
    ... I have recommended using --TEXTto ensure that the resulting time value is identical to the binary form of the displayed time value. ... Excel first rounds to the second, then it truncates to the minute. ... In contrast, TEXTconsistently truncates to the hour. ...
    (microsoft.public.excel.worksheet.functions)
  • Re: real basic..
    ... "by convention, integer division always rounds down, even in cases like ... Sta parlando della stessa cosa, ma prima diceva "rounds down", ora ... esorta a ricordare che "truncates". ...
    (it.comp.macintosh)
  • Re: Cant find her!
    ... The enumerator must have ... > had a few nips whilst making his rounds. ... Ada becomes Alfred? ...
    (soc.genealogy.britain)