From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,fee8802cc3d8334d X-Google-Attributes: gid103376,public X-Google-Thread: 10a146,fee8802cc3d8334d X-Google-Attributes: gid10a146,public From: nabbasi@pacbell.net.NOSPAM Subject: Re: Ada and Java. different behaviour. casting long to int problem. Date: 1999/06/12 Message-ID: <7jt69l$143f@drn.newsguy.com>#1/1 X-Deja-AN: 488725702 References: <7jt2c0$vrb@drn.newsguy.com> Organization: Newsguy News Service [http://www.newsguy.com] Newsgroups: comp.lang.ada,comp.lang.java.programmer Date: 1999-06-12T00:00:00+00:00 List-Id: In article <7jt2c0$vrb@drn.newsguy.com>, nabbasi@pacbell.net says... >In the example, I cast a long integer literal, outside the range >of an integer, into an integer variable. Java allows this, >and produces the wrong result. Ada compiler detect this as an error >at compile time, and it also detects the same thing at run-time. > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ actually that was an error of mine, it did not detect it at run-time, only at compile time. see below >------------------- ada example 2 ----------------------------- > >with Ada.Text_Io; USE Ada.Text_Io; > >procedure Test_Cast is > N : Long_Integer; > M : Integer; > package Long_Int_Io is new Ada.Text_Io.Integer_Io(Long_Integer); >begin > Long_Int_Io.Get(N); > M := Integer (N); > > Put_Line ( "long n =" & Long_integer'Image(N) > & "int m =" & Integer'Image(M) ); >end Test_Cast; > >$./test_cast <--- run >3200000000 > >raised ADA.IO_EXCEPTIONS.DATA_ERROR <-- exception raised >------------------------------------------------------------- This was an input error which I do not understand now why. This is a modified Ada program, that shows Ada did not detect at run-time the overflow (I think may be I am not using the correct GNAT flag to enable run-time chekcing? I need to check more on this) ------------------------------------- with Ada.Text_Io; USE Ada.Text_Io; procedure Test_Cast is N : Long_Integer; M : Integer; package Long_Int_Io is new Ada.Text_Io.Integer_Io(Long_Integer); begin N := 2000000000; M := Integer (N); M := M*2; Put_Line ( "long n =" & Long_integer'Image(N) & "int m =" & Integer'Image(M) ); end Test_Cast; ------------------------------------------ $./test_cast long n = 2000000000int m =-294967296 ^^^^^^^^^^^^^^ ??? Nasser