uCalc API Version: 2.1.3-preview.2 Released: 6/17/2026

Warning

uCalc API Preview Release Notice:The documentation describes the intended behavior of the API. The current preview build contains incomplete features, unoptimized performance, and is subject to breaking changes.

ValueInt16()

Method

Product: 

Fast Math Parser

Class: 

Item

Performs a direct memory read of a variable's value, reinterpreting the first 16 bits as a signed integer.

Syntax

ValueInt16()

Parameters

[None]

Return

int16

Returns a 16-bit integer

Remarks

The ValueInt16() method is a low-level, high-performance accessor that reads the value of a uCalc Item by interpreting its first 16 bits (2 bytes) of memory as a signed integer (short in C#).

⚠️ Direct Memory Interpretation

This method does not perform type checking or conversion. It is a direct memory operation.

  • Correct Usage: When called on a variable that was defined as Int16, it correctly returns the variable's value.
  • Incorrect Usage (Type Mismatch): When called on a variable of a different data type, this method performs a direct, low-level memory read. It reinterprets the first 2 bytes of the variable's stored value as a 16-bit signed integer, which can lead to unpredictable or meaningless results if the underlying data types are not compatible.
    • On an Int32: It will read the lower 16 bits of the 32-bit integer.
    • On a Double: It will read the first 2 bytes of the 8-byte floating-point representation, resulting in a value that is not numerically related to the original double.
    • On a String: It will read the first 2 bytes of the internal string object's memory (which could be part of a pointer or length field), leading to unpredictable results.

For safe, type-converting access, use ValueStr() and parse the resulting string.

💡 Comparative Analysis

This method's behavior is analogous to low-level type punning operations in other languages, designed for performance at the cost of safety.

  • vs. C++ reinterpret_cast: Functionally similar to casting a pointer of one type to a short* and dereferencing it. It's a powerful tool for performance-critical code but bypasses the type system.
  • vs. C# BitConverter.ToInt16: Similar to taking the first two bytes from another type's byte array and converting them to a short.

Use this method only when performance is critical and the data type is guaranteed.

Examples