octave-bug-tracker
[Top][All Lists]

## [Octave-bug-tracker] [bug #53128] Warn about implicit casts from int to

 From: Benjamin Buch Subject: [Octave-bug-tracker] [bug #53128] Warn about implicit casts from int to double Date: Tue, 13 Feb 2018 04:34:59 -0500 (EST) User-agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:58.0) Gecko/20100101 Firefox/58.0

```Follow-up Comment #6, bug #53128 (project octave):

I will try to write it a little more clearly and answer the questions.

> Wait, why would you write a doubly nested for loop to multiply a matrix by a
scalar?

This is a simplified test case which should illustrate the problem. Real code
would of course be more complex and could not be shortened.

> In any case, I believe the reason that Matlab (and, therefore, Octave)
treats mixed double/int operations as int is because a constant like "2" is
actually a double precision object (and it's highly unlikely that's going to
change).

Correct.

> However, maybe we could have a warning for mixed double/int operations when
the value of the double is not an exact integer. Is that what you are asking
for?

No.

> Or do you really want a warning for any mixed int/double operations?

No.

> It would be really inconvenient for expressions like "some_int8_value * 2"
to result in a double value instead of an int8 value.

I'm not proposing that. It would break code and compatibility with Matlab.

Here's what I propose: Warn about implicit casts from int to double.

I will try to illustrate it again with the example:

img = uint8(ones([600, 800]));
factor = 1.7;

img_size = size(img);
result = zeros(img_size); % type is double

for c = 1:img_size(2)
for r = 1:img_size(1)
result(r,c) = img(r,c) * factor;
%^ integer multiplication: okay
%^ implicit cast from int to double: warning!
end
end

% result has type double an values 2

It is the implicit cast where I want to issue a warning, because in most cases
it is a result of wrong codes. The error is very difficult to recognize,
because in the working environment result is written as a matrix of type
double and in a more complex example may also contain comma numbers.

The situation changes fundamentally if result is not defined:

img = uint8(ones([600, 800]));
factor = 1.7;

img_size = size(img);
% result = zeros(img_size); % result is implicitly defined in the loop

for c = 1:img_size(2)
for r = 1:img_size(1)
result(r,c) = img(r,c) * factor;
%^ integer multiplication: okay
%^ define, resize and assign result: okay
end
end

% result has type unit8 an values 2

In the working environment result is listed as a uint8 matrix. No warning is
necessary here, as the error is much easier to recognize.

from int to double.

More examples:

a = uint8(5);              % okay
b = zeros([1, 1])          % okay
b(1, 1) = uint8(5)         % warning
b = ones([1 1]) * uint8(5) % okay

_______________________________________________________

<http://savannah.gnu.org/bugs/?53128>

_______________________________________________
Message sent via/by Savannah
http://savannah.gnu.org/

```