Short

From Microduino Wiki
Revision as of 09:56, 18 August 2016 by Fengfeng (talk) (Created page with "short single precision floating point<br> *'''Description'''<br> Float, floating-point data, is a number with a decimal point. Floating-point numbers are generally used to im...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

short single precision floating point

  • Description

Float, floating-point data, is a number with a decimal point. Floating-point numbers are generally used to imitate approximately continuous values, because they have larger precision than integers. The range of the floating-point value is 3.4028235 E+38 ~ -3.4028235E +38. It is stored as 32-bit(4 bytes) information.

Float has only 6-7 bits of valid numbers, which refers to the total bits rather than the numbers on the right of the decimal point. Different from other platform, you can use double type to get more accurate result there(such as 15 bits), in Arduino, the size of double is same with float.

Floating-point numbers are not accurate in some cases, when compare data, it may result in weird result. For example, 6.0 / 3.0 may not equal to 2.0. You should make the absolute value of the difference between two numbers less than some small numbers, so that you can get the result that the two numbers are approximately equivalent.

The operating speed of floating-point is much slower than that of int, for example, if the loop has a critical timing function, and needs to run at a fast speed, you should avoid the floating-point calculation. Programmers usually use longer forms to transform the floating-point calculation into int to promote the speed.

  • Example
    float myfloat;
    float sensorCalbrate = 1.117;
  • Syntax
float var = val;

var——the name of your float variable
val——the value to be assigned to the variable

  • Sample code
   int x;
   int y;
   float z;
 
   x = 1;
   y = x / 2;         // Y is 0, because integer cannot accommodate scores. 
   z = (float)x / 2.0;   // Z is 0.5(You must use 2.0 as the divisor, rather than 2.)

[Return to Arduino Syntax Manual]