# EasyC Variables Question

I am trying to do some trigonometry within EasyC and I have ran into a few questions.

When using a trig function, EasyC recommends using a ‘double’ variable type.
The ‘double’ variable type has a range of 2e^-308 to 2e^+308, including 6 decimal points. However, this is a 64 bit variable.
Is there any reason I shouldn’t use a ‘float’ variable type instead? It has a range of e^-38 to e^38, including 6 decimal points. Trig functions fall well within this range.
I know the extra 32 bits isn’t much, but i figure it’s good practice to use memory as efficiently as possible.
Would there be any unforeseen side effect to using a ‘float’ instead of a ‘double’ for trig functions?

Secondly, how well do non-decimal carrying variable types handle being assigned values that have a decimal tail?
For example, say I have the ‘float’ variable “A” that I have already limited between -127 and 127.
Say “B” is a ‘char’ variable type.
What happens if I do:
A=B
If “B” has a decimal value is it truncated or rounded? Or does something bad happen?

If the lack of precision for 32 bits is acceptable, use float instead. If the trig functions expect doubles, don’t forget to Cast the float variables.

IIRC, this is implementation specific… make sure to Cast your variables, that should make everything correct.

What is the proper syntax in EasyC V4 for casting variables?
A=(char) B ?

Also, what do you mean by a double is more precise than a float? Don’t both hold values with up to 6 decimal places? A double can just hold a number much larger than a float, correct?

The Cast is used to promote the R-Value ( the Right Side of the Equal Sign ) to the Type of the L-Value ( the Left Side of the Equal Sign ).

Since ‘B’ is an 1 Byte ( 8 bit ) Int Type and ‘A’ is a 4 Byte ( 32 bit ) Float Type, the Compiler should be able to make an Implicit Cast, but you can “help it”, by writing your statement as A = ( float ) B;, which tells the compiler to make a temporary copy of B as a Float, and assign it to ‘A’. ( See Type Conversion for more info )

In the above example, ‘B’ can not have a Decimal Value because it is a Char, which is an Integer Type.

Going the other way, B = A;, can have very unpredictable results, from Compiler to Compiler. And I would guess from Architecture Endianness as well. ( See Endianness )

The Cast is needed to make sure the correct bits are coped from the R-Value to the L-Value. B = ( char ) A; If the R-Value is Larger than the L-Value’s Maximum, your Results might be Unpredictable. Also, the conversion from a Float to an Int usually results in a Rounding Down, because the Decimal Portion is usually Truncated, leaving just the Integer Portion.

If the GCC Compiler used by EasyC follows IEEE 754, they have a different number of Decimal Places and Largest Value.

``````* Sign bit: 1 bit
* Exponent width: 8 bits
* Significand precision: 24 (23 explicitly stored)
``````

The IEEE 754 standard specifies a binary64 as having:
* Sign bit: 1 bit
* Exponent width: 11 bits
* Significand precision: 53 bits (52 explicitly stored)

If the Float has enough Precision for your calculation, use it… It will save Processing Time as well as Memory ( RAM ).

Precision is the main difference where double is a double precision (64 bit) floating point data type and decimal is a 128-bit floating point data type.

Double - 64 bit (15-16 digits)
Decimal - 128 bit (28-29 significant digits)

So Decimals have much higher precision and are usually used within monetary (financial) applications that require a high degree of accuracy. But in performance wise Decimals are slower than double and float types. Double Types are probably the most normally used data type for real values, except handling money. More about…Decimal vs Double vs Float

Johnson

I’m pretty sure any variable type with decimal points will eventually start losing precision because the number becomes too large and starts eating into the decimal places’ memory. Either that, or they start getting rounded to a power of two somewhere. I’m not too sure on that, but whatever happens will occur at lower values and more noticeably with floats than doubles. If someone actually understands what happens under the hood here, I don’t, so feel free to pitch in.