|
From: Ethan M. <eam...@gm...> - 2017-11-14 23:07:35
|
On Tue, Nov 14, 2017 at 2:19 PM, theozh <th...@gm...> wrote: > Thanks Ethan, > I knew the int() function but didn't know about the real() function. I thought this is for extracting the real part of complex numbers, but obviously it can also be used to convert an integer number to a real number. > >> [shrug] integer division is "normal" for most programming and >> scripting languages. > [ahem]... then the few languages I know a little bit (Perl, Python, Pascal,... ) are not "normal" ;-) I think you are mis-remembering > stonelion [163] python > Python 2.7.13 (default, Jan 28 2017, 17:29:29) > [GCC 5.4.0] on linux2 > Type "help", "copyright", "credits" or "license" for more information. > >>> print 5/2 > 2 > >>> > > Perl being perl, you have your choice :-) Although it is true that perl defaults to floating division, it is recommended to place the "use integer" pragma at the start of any program that cares. stonelion [164] perl > use integer; > print 5/2, "\n"; > 2 > > Pascal (ah nostalgia...) it's been years. I no longer remember, and I don't have a Pascal compiler on my current machines to try it. > What worries me a bit is: > Quote help: > "The result of division of a negative integer by a positive one may vary among compilers. Try a test like "print -5/2" to determine if your system chooses -2 or -3 as the answer." I suspect that statement dates all the way back to 1980something, and refers to compilers or computer architectures from last century. C99 compliance guarantees truncation towards zero. (But C89 did not, so yes at the time gnuplot was first written it made sense to worry about this). As a side note - people contributing code to gnuplot recently often assume that use of C99 features is OK. We may want to require use of a C99 compliant complier in the future, but so far the gnuplot coding guidelines and installation instructions don't say that. If we add that caveat for 5.3, I will remove the caution you point to in the docs. > Well, as long as I am working only on my system I should know what I will get ;-). Anyhow, using real(variable) will force floating point arithmetic if you need that. |