[Seed7-users] First questions
Interpreter and compiler for the Seed7 programming language.
Brought to you by:
thomas_mertes
From: Thomas M. <tho...@gm...> - 2012-09-11 21:02:25
|
On Thu, 6 Sep 2012 00:03:09 +0200, Renato Lenzi <re...@gm...> wrote: > 1) i have to do this simple task: 4 div 3. Ok, i get 1. But what have > i to do if i'd like to obtain 1,333333333 instead of 1? 4 and 3 are integer numbers and div is the integer division operator. For a floating point division you need floating point numbers and the / division operator. E.g.: 4.0 / 3.0 If you have integer variables and the division should have a float result you need to convert the integers to float first. Seed7 does not support implicit type conversions so the the conversion must be done with the flt function: flt(dividend) / flt(divisor) > 2) why / is ok for floats but if i want to perform a division while > with integers i have to use "div"? That is why there are different > operators? This was inherited from Pascal/Modula/Ada. Additionally it opens the opportunity to create a rational number with / (see below). Note that there is another integer division operator mdiv, which acts different form div: div divides and truncates towards 0 mdiv divides and rounds downward towards minus infinite. For positive numbers div and mdiv give the same result. The result just differs when dividend or divisor (or both) are negative. There are also two remainder operators: rem which computes the remainder of a div division. mod which computes the modulo, which is the remainder of mod. The chapter describing the type integer contains tables for the behavior of 'div', 'rem', 'mdiv' and 'mod': http://seed7.sourceforge.net/manual/types.htm#integer The / operator could be overloaded to work for integers with: const func integer: (in integer: dividend) / (in integer: divisor) is action "INT_DIV"; In this case 4/3 would return 1. I decided to omit this definition from the predefined language. Alternatively the / operator could be overloaded to convert integers to float and to do the division afterwards: const func float: (in integer: dividend) / (in integer: divisor) is return flt(dividend) / flt(divisor); In this case 4/3 would return the float value 1.333333333 . I decided also against this possibility, because it looks like an implicit conversion. Instead the / operator, with two integer operands, is used to generate a rational number. This is defined in the library rational.s7i: http://seed7.sourceforge.net/libraries/rational.htm#%28in_integer%29/%28in_integer%29 This way, when rational.s7i is included, the statement writeln(4/3 digits 6) writes 1.333333 Rational numbers use a 32-bit integer numerator and denominator. Therefore they are rather limited (integer overflows are not checked). Alternatively there is the type bigInteger (defined in bigint.s7i), which allow writeln(4_ / 3_ digits 50) The statement above writes 1.33333333333333333333333333333333333333333333333333 What do you think about this design decisions? BTW: I already sent this answer directly to Renato Lenzi. I just forgot to send it to this group also. So I do it now. BTW 2: I added an answer to this question to the FAQ. see: http://seed7.sourceforge.net/faq.htm#div_operator Regards, Thomas Mertes |