ArrayFromFP8

uchar类型数组复制到给定格式的float或double类型的数组中。

bool   ArrayFromFP8(
   const float&         dst_array[],        // 复制到
   const uchar&         src_array[],        // 复制自
   ENUM_FLOAT8_FORMAT   fmt                 // 格式
   );

double类型的重载

bool   ArrayFromFP8(
   const double&        dst_array[],        // 复制到
   const uchar&         src_array[],        // 复制自
   ENUM_FLOAT8_FORMAT   fmt                 // 格式
   );

参数

dst_array[]

[out]  接收器数组或float或double类型。

src_array[]

[in]  uchar类型的源数组。

fmt

[in]  来自ENUM_FLOAT8_FORMAT枚举的复制格式。

 

返回值

如果成功返回true,否则返回false。

注意

所有类型的FP8格式均在ENUM_FLOAT8_FORMAT枚举中定义,在MQL5中仅用于ONNX模型的操作。

如果从OnnxRun函数执行得到的输出参数为来自ENUM_FLOAT8_FORMAT枚举的FP8,则可以使用该函数将结果转换为float或double数组。

FP8(8位浮点)是用于表示浮点数的数据类型之一。在FP8中,每个数字由8个数据位表示,通常分为三个部分:符号、指数和尾数。这种格式提供了准确性和存储效率之间的平衡,使其对于需要内存和计算效率的应用程序具有吸引力。

通过采用紧凑型数字表示法,FP8减少了内存需求并加速了计算。此外,FP8可用于实现算术计算和信号处理等低级操作。

示例:函数来自文章使用float16和float8格式的ONNX模型

//+------------------------------------------------------------------+
//| RunCastFloat8Float                                               |
//+------------------------------------------------------------------+
bool RunCastFloat8ToFloat(long model_handle,const ENUM_FLOAT8_FORMAT fmt)
  {
   PrintFormat("TEST: %s(%s)",__FUNCTION__,EnumToString(fmt));
//---
   float test_data[15]   = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
   uchar data_float8[15] = {};
   if(!ArrayToFP8(data_float8,test_data,fmt))
     {
      Print("error in ArrayToFP8. error code=",GetLastError());
      OnnxRelease(model_handle);
      return(false);
     }
   U<ucharinput_float8_values[3*5];
   U<floatoutput_float_values[3*5];
   float    test_data_float[];
//--- 将float8转换为float
   if(!ArrayFromFP8(test_data_float,data_float8,fmt))
     {
      Print("error in ArrayFromFP8. error code=",GetLastError());
      OnnxRelease(model_handle);
      return(false);
     }
   for(uint i=0i<data_float8.Size(); i++)
     {
      input_float8_values[i].value=data_float8[i];
      PrintFormat("%d input value =%f  Hex float8 = %s  ushort value=%d",i,test_data_float[i],ArrayToHexString(input_float8_values[i].uc),input_float8_values[i].value);
     }
   Print("ONNX input array: ",ArrayToString(input_float8_values));
//--- 执行模型(使用ONNX将float8转换为float)
   if(!OnnxRun(model_handle,ONNX_NO_CONVERSION,input_float8_values,output_float_values))
     {
      PrintFormat("error in OnnxRun. error code=%d",GetLastError());
      OnnxRelease(model_handle);
      return(false);
     }
   Print("ONNX output array: ",ArrayToString(output_float_values));
//--- 计算误差(比较ONNX和ArrayFromFP8结果)
   double sum_error=0.0;
   for(uint i=0i<test_data.Size(); i++)
     {
      double delta=test_data_float[i]-(double)output_float_values[i].value;
      sum_error+=MathAbs(delta);
      PrintFormat("%d output float %f = %s difference=%f",i,output_float_values[i].value,ArrayToHexString(output_float_values[i].uc),delta);
     }
//---
   PrintFormat("%s(%s): sum_error=%f\n",__FUNCTION__,EnumToString(fmt),sum_error);
   return(true);
  }

另见

ArrayToFP8ArrayCopy