[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
- Next message: Bob Koehler: "Re: Anyone Using Multinet NFS to access CMS Libraries?"
- Previous message: Chip Coldwell: "Re: another CXX question"
- In reply to: Dave Froble: "Re: Platform Support vs. Business Support"
- Next in thread: Tom Linden: "Re: [OT] Rounding v Truncation, was: Re: Platform Support vs."
- Reply: Tom Linden: "Re: [OT] Rounding v Truncation, was: Re: Platform Support vs."
- Reply: Dave Froble: "Re: [OT] Rounding v Truncation, was: Re: Platform Support vs."
- Reply: Alex van Denzel: "Re: [OT] Rounding v Truncation, was: Re: Platform Support vs."
- Maybe reply: O'Brien Paddy: "RE: [OT] Rounding v Truncation, was: Re: Platform Support vs."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: Bob Koehler: "Re: Anyone Using Multinet NFS to access CMS Libraries?"
- Previous message: Chip Coldwell: "Re: another CXX question"
- In reply to: Dave Froble: "Re: Platform Support vs. Business Support"
- Next in thread: Tom Linden: "Re: [OT] Rounding v Truncation, was: Re: Platform Support vs."
- Reply: Tom Linden: "Re: [OT] Rounding v Truncation, was: Re: Platform Support vs."
- Reply: Dave Froble: "Re: [OT] Rounding v Truncation, was: Re: Platform Support vs."
- Reply: Alex van Denzel: "Re: [OT] Rounding v Truncation, was: Re: Platform Support vs."
- Maybe reply: O'Brien Paddy: "RE: [OT] Rounding v Truncation, was: Re: Platform Support vs."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|