Spec-Zone.ru › Octave 8

4.7 Автоматическое преобразование типов данных

Многие операторы и функции могут работать с различными типами данных. Например,

uint8 (1) + 1
    ⇒ 2


single (1) + 1
    ⇒ 2


min (single (1), 0)
   ⇒ 0

где результаты соответственно имеют типы uint8, single и single. Это сделано для совместимости с MATLAB. Действительные смешанные операции определены следующим образом:

Смешанная операция Результат
double OP single single
double OP integer integer
double OP char double
double OP logical double
single OP integer integer
single OP char single
single OP logical single

Когда функции ожидают double, но им передаются другие типы, автоматическое преобразование зависит от функции:

a = det (int8 ([1 2; 3 4]))
    ⇒ a = -2
class (a)
    ⇒ double


a = eig (int8 ([1 2; 3 4]))
    ⇒ error: eig: wrong type argument 'int8 matrix'

Когда два операнда являются целыми числами, но разной ширины, в некоторых случаях они преобразуются к большей разрядности, а в других случаях возникает ошибка:

a = min (int8 (100), int16 (200))
    ⇒ 100
class (a)
    ⇒ int16


int8 (100) + int16 (200)
   ⇒ error: binary operator '+' not implemented
   for 'int8 scalar' by 'int16 scalar' operations

Для двух целочисленных операндов они обычно должны быть оба со знаком или оба без знака. Смешение со знаком и без знака обычно приводит к ошибке, даже если они имеют одинаковую разрядность.

min (int16 (100), uint16 (200))
   ⇒ error: min: cannot compute min (int16 scalar, uint16 scalar)

В случае смешанных индексированных присваиваний тип не изменяется. Например,

x = ones (2, 2);
x(1, 1) = single (2)
   ⇒ x = 2   1
          1   1

где x остается двойным типом точности.

© 1996–2023 The Octave Project Developers
Permission is granted to make and distribute verbatim copies of this manual provided the copyright notice and this permission notice are preserved on all copies.
Permission is granted to copy and distribute modified versions of this manual under the conditions for verbatim copying, provided that the entire resulting derived work is distributed under the terms of a permission notice identical to this one.
Permission is granted to copy and distribute translations of this manual into another language, under the above conditions for modified versions.
https://docs.octave.org/v8.1.0/Automatic-Conversion-of-Data-Types.html

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API