* [Caml-list] out-of-bound array or string access
@ 2001-10-10 10:12 Johann Spies
2001-10-10 10:31 ` Alain Frisch
0 siblings, 1 reply; 2+ messages in thread
From: Johann Spies @ 2001-10-10 10:12 UTC (permalink / raw)
To: ocaml mailing list
When I run this small program I get the following results:
------------------------
$ ./paaiement 10000 13.5 5
Die paaiement is R230.10
--------------------------
All arguments correct. Result as expected...
--------------------------
$ ./paaiement 10000 -13.5 5
Die rentekoers (-13.500000) moet tussen 0 en 100.0 wees
-------------------------
Second argument negative - not acceptable. Output as expected.
-------------------------------
$ ./paaiement 10000 -13.5 5 6
Gebruik: paaiement <bedrag> <rentekoers p.j.> <aantal_jare>
--------------------------------
Arguments > 4 - output as expected.
-------------------------------
$ ./paaiement 10000 -13.5
Fatal error: uncaught exception Invalid_argument("out-of-bound array
or string access")
---------------------------------
Arguments < 4 - unexpected error.
Why did "if Array.length Sys.argv <> 4 then " not catch this?
The code:
-------------------------------------------
let maandelikse_koers koers = koers /. 100.0/. 12.0;;
let n jare = jare *. 12.0;;
let paaiement rentekoers jare bedrag =
(bedrag *. (maandelikse_koers rentekoers) /. (1.0 -. exp
((-.n jare) *. log (1.0 +. (maandelikse_koers rentekoers)))));;
let bedrag = float_of_string Sys.argv.(1);;
let rentekoers = float_of_string Sys.argv.(2);;
let jare = float_of_string Sys.argv.(3);;
let main() = if Array.length Sys.argv <> 4 then
begin
print_string
"Gebruik: paaiement <bedrag> <rentekoers p.j.> <aantal_jare>\n";
print_newline();
exit 2
end
else
if rentekoers >= 0. then
Printf.printf "\nDie paaiement is R%04.2f\n"
(paaiement rentekoers jare bedrag)
else
begin
Printf.printf
"Die rentekoers (%f) moet tussen 0 en 100.0 wees" rentekoers ;
print_newline();
exit 2;
end ;;
main();
print_newline();
-------------------------------------------------
I am again missing something here.
Regards
Johann
--
Johann Spies Telefoon: 021-808 4036
Informasietegnologie, Universiteit van Stellenbosch
"He that covereth his sins shall not prosper; but whoso
confesseth and forsaketh them shall have mercy."
Proverbs 28:13
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [Caml-list] out-of-bound array or string access
2001-10-10 10:12 [Caml-list] out-of-bound array or string access Johann Spies
@ 2001-10-10 10:31 ` Alain Frisch
0 siblings, 0 replies; 2+ messages in thread
From: Alain Frisch @ 2001-10-10 10:31 UTC (permalink / raw)
To: Johann Spies; +Cc: ocaml mailing list
On 10 Oct 2001, Johann Spies wrote:
> Why did "if Array.length Sys.argv <> 4 then " not catch this?
>
> The code:
...
> let bedrag = float_of_string Sys.argv.(1);;
> let rentekoers = float_of_string Sys.argv.(2);;
> let jare = float_of_string Sys.argv.(3);;
>
> let main() = if Array.length Sys.argv <> 4 then
...
Toplevel phrases are executed in order, so the code want to access
Sys.argv.(1,2,3) before the test.
--
Alain Frisch
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2001-10-10 10:31 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-10-10 10:12 [Caml-list] out-of-bound array or string access Johann Spies
2001-10-10 10:31 ` Alain Frisch
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).