Version: 2022.3
LanguageEnglish
  • C#

Mathf.FloatToHalf

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Switch to Manual

Declaration

public static ushort FloatToHalf(float val);

Parameters

val The floating point value to convert.

Returns

ushort The converted half-precision float, stored in a 16-bit unsigned integer.

Description

Encode a floating point value into a 16-bit representation.

Converting a floating point value to a half causes it to lose precision and also reduces the maximum range of values it can represent. The new range is from -65,504 and 65,504. For more information on 16-bit floating-point numbers, and for information on how precision changes over the range of values, see Half-precision floating-point format.

If the converted floating point value falls exactly between two half-precision values, this method rounds it to the value furthest from zero (Round away from zero tie-break rule). This selects the value closer to positive or negative infinity, depending on the sign.

You should only use the returned ushort as a storage format. If you want to perform mathematical operations on it, first convert it back to a float with Mathf.HalfToFloat.

Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961