Vim skill training tutorial (16) - floating point number calculation function

Floating point calculation function

All functions in this section are valid only when vim is compiled with +float supported.

Trigonometric functions

  • sin() : sine sine function
  • cos() : cosine cosine function
  • tan() : tangent tangent function
  • asin() : arc sine arcsine function
  • acos() : arc cosine arc cosine function
  • atan() : arc tangent function
  • atan2({X coordinate},{Y coordinate}) : arc tangent function
  • sinh() : hyperbolic sine hyperbolic sine function
  • cosh() : hyperbolic cosine hyperbolic cosine function
  • tanh() : hyperbolic tangent hyperbolic tangent function

These functions are basically the encapsulation of the corresponding C functions. For example, if you are not familiar with hyperbolic sine, you can refer to man sinh. If you don't understand atan2, you can man atan2.

Mathematical calculation

  • abs(): absolute value
  • fmod(): floating point remainder
  • exp(): power of e
  • log(): natural logarithm
  • log10(): base 10 logarithm
  • pow(): factorial
  • sqrt(): Square root, returns NaN if taking the square root of a negative number.
  • isnan(): Determines whether it is NaN. For example isnan(0.0/0.0) is true.

Floating point to integer

  • float2nr() : Convert float to integer.
  • round() : round off, round up
  • ceil(): round up, round up
  • floor(): round down, round down
  • trunc(): simply truncate the number after the decimal point

Let's write a function to test their functionality:

function Float2Number(arg)
        echo "Orginal value:"
        echo a:arg
        echo "float2nr:"
        echo float2nr(a:arg)
        echo "round:"
        echo round(a:arg)
        echo "ceil:"
        echo ceil(a:arg)
        echo "floor:"
        echo floor(a:arg)
        echo "trunc:"
        echo trunc(a:arg)
endfunction

Let's try 2.06 first:

Orginal value:
2.06
float2nr:
2
round:
2.0
ceil:
3.0
floor:
2.0
trunc:
2.0

Let's try 122.667 again:

Orginal value:
122.667
float2nr:
122
round:
123.0
ceil:
123.0
floor:
122.0
trunc:
122.0

Let's look at another negative number: -9.08

Orginal value:
-9.08
float2nr:
-9
round:
-9.0
ceil:
-9.0
floor:
-10.0
trunc:
-9.0

Finally, a negative number with five entries:

Orginal value:
-65.96
float2nr:
-65
round:
-66.0
ceil:
-65.0
floor:
-66.0
trunc:
-65.0

Let's summarize:
* Except for float2nr, all other truncated results are floating point numbers
* The strategy of float2nr is consistent with trunc, that is to say, if you want to round, do round first and then float2nr.
* round is rounding, no matter whether it is positive or negative, all is rounded up.
* ceil takes the smallest integer greater than or equal to it.
* floor takes the largest integer less than or equal to it.
* trunc simply removes the fractional part.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325696952&siteId=291194637