answersLogoWhite

0


Best Answer

Integers are always guaranteed to be precise, provided you have sufficient bits to represent the number (plus the sign, if required). All logical and mathematical operations are extremely efficient when working with integers, even with division which simply truncates any fraction. If that's all the precision you need then you don't need to use a float. But if you need real numbers, then you must use a float.

Floating point numbers are never guaranteed to be accurate. Irrational Numbers (such as pi and 1/3) are impossible to represent fully within a finite number of bits, thus there has to be a degree of precision. All floating point numbers are actually represented in computer memory using the binary equivalent of scientific notation, so even when dealing with small integer values, logic errors can creep in (such as 0.9 + 0.1 == 1.000000...01). Thus when working with floats you have to accommodate for the degree of precision.

Due to the increased complexity of floating point numbers, they are much less efficient than when working solely with integers. Thus if you can guarantee that a floating point number can be accurately represented as an integer (no fractions), then it makes more sense to use an integer. However, if you cannot make that guarantee then you must use a float.

Integer division can be problematic due to truncation, so it's often necessary to cast integers to floats to obtain a higher degree of precision, rounding the result as appropriate before casting back to an integer. Repeatedly converting back and forth can be costly, so it's sometimes better to just work with a float from the outset.

User Avatar

Wiki User

11y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Why you use integer variables when the value of float and integer is same?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is variable in c plus plus programming?

You declare a variable by first defining its data type and then its name followed by a semi-colon. Here is an example: int variable; The example above declares an uninitialized integer variable. You can initialize the variable by giving it a value such as "int variable = 1;". It is important to initialize your variables, because you can get errors when executing your program that the variable does not have a value or is uninitialized. Variables that are uninitialized have whatever garbage value happens to be when the program is executed. Here are all of the data types that a variable can be: *int - integer value *char - character value *bool - boolean value


What Are The Differences Between Java And Python Variables Declaration?

You cannot declare a variable in Python without assigning some value to it. This is simply because a variable's type is inferred from the type of value that you assign to it. a = "hello world" # a is a string b = 42 # b is an integer c = 3.14 # c is a float d = [4, 8, 15, 16, 23, 42] # d is an integer array In Java, all variable declarations must explicitly include a type even when declared with an initial value. The initial value, when given, must be of the same type or of a type that is covariant with the variable's type. String a = "hello world"; int b = 42; double c = 3.14 int[] d = {4, 8, 15, 16, 23, 42} Note that local variables in Java that are declared but not assigned a value are considered "uninitialised". You must assign a value to all local variables before using them. Instance variables (members of a class) are always initialised. That is; numeric types have an initial value of zero, Boolean types are false and object references are null.


What is type equivalence in programming language?

Type equivalence occurs when two variables are of the same type. For example, if both variables are int (integer variables), they are of equal types. Equivalence can also occur with two different types that are compatible with each other.


What are the variables in vb?

The variables is visual basic are the items being declared in order to use within the program, for example if you were writing a program for the SUVAT equations the variables would be difined as follows... Dim is as integer Dim iu as integer Dim iV as integer e.c.t Think of the Dim to mean declare and the "as integer" is the data type. If it was a text value (string) for example a name it would be declared as follows: Dim sname as string where sname is the variable. For an array of values you would do the same but as follows: dim sname(100) as string this would create a space so to speak for 101 (0-100) names which would be inputted as so: sname(0) = Dan sname(1) = Sarah e.c.t hope that helps without being to complicated


What are Integer Boolean and Double wrapper classes?

In Java an integer and a double are "primitive" data types.When dealing with functions, often you want to do more than send an "int" or a "double"'s VALE, you wish to send a REFERENCE of a number type to a function so that you can operate on that number type. What you can do is create a new Integer, Boolean, or Double, and send it to the function. This creates a "CLASS" that acts as an Integer (or whatever you chose).Eg. You want a function that takes in a number variable, and adds two to it, and returns NOTHING. You cannot send a normal "int", because that integer cannot be "referenced" to after the function call. You would create a new Integer Object from the Integer Class, and insert that new object to the function.Integer four = new Integer(4); // Makes a new Integer named "four" with the value 4.There is a wrapper class for every primitive in Java. For instance, the wrapper class for int is Integer, the class for float is Float, and so on. Remember that the primitive name is simply the lowercase name of the wrapper except for char, which maps to Character, and int, which maps to Integer.The Wrapper classes for each of the primitive types is as follows:1. boolean - Boolean2. byte - Byte3. char - Character4. double - Double5. float - Float6. int - Integer7. long - Long8. short - ShortA point to note here is that, the wrapper classes for numeric primitives have the capability to convert any valid number in string format into its corresponding primitive object. For ex: "10" can be converted into an Integer by using the below lineInteger intVal = new Integer("10");I can do the same for all numeric types like long, short etc. But, if I pass an invalid value I will get a "NumberFormatException" Ex:Integer intVal = new Integer("Haha");The Wrapper ConstructorsAll of the wrapper classes except Character provide two constructors: one that takes a primitive of the type being constructed, and one that takes a String representation of the type being constructed-for exampleInteger i1 = new Integer(42);Integer i2 = new Integer("42");orFloat f1 = new Float(3.14f);Float f2 = new Float("3.14f");The Character class provides only one constructor, which takes a char as an argument-for exampleCharacter c1 = new Character('c');The constructors for the Boolean wrapper take either a boolean value true or false, or a String. If the String's case-insensitive value is "true" the Boolean will be true-any other value will equate to any other value will equate to false. Until Java 5, a Boolean object couldn't be used as an expression in a boolean test-for instanceBoolean b = new Boolean("false");if (b) // won't compile, using Java 1.4 or earlierAs of Java 5, a Boolean object can be used in a boolean test, because the compiler will automatically "unbox" the Boolean to a boolean.

Related questions

When is the sum of a positive integer and a negative integer zero?

When the absolute value of the positive integer is the same as the absolute value of the negative one.


How is subtracting a negative integer related to its absolute value and addition?

Subtracting a negative integer is the same as adding its absolute value.


What integer has the same absolute value as-15?

15


Can you have two variables in the same equation with the same value?

Yes, but it is not a common occurrence.


Can two integers can have the same absolute value?

Yes, a positive integer and its negative have the same absolute value. For example, +5 and -5 have the same absolute value.


Can two integers have the same absolute value?

Yes, a positive integer and its negative have the same absolute value. For example, +5 and -5 have the same absolute value.


How do you factor percentages?

If the percentage is an integer value, it can be factored in the same way as any other integer and if it is not, it cannot be factored.


What is variable in c plus plus programming?

You declare a variable by first defining its data type and then its name followed by a semi-colon. Here is an example: int variable; The example above declares an uninitialized integer variable. You can initialize the variable by giving it a value such as "int variable = 1;". It is important to initialize your variables, because you can get errors when executing your program that the variable does not have a value or is uninitialized. Variables that are uninitialized have whatever garbage value happens to be when the program is executed. Here are all of the data types that a variable can be: *int - integer value *char - character value *bool - boolean value


What integer does not have the same absolute value of any other number?

Zero.


Is It true two different integer can have the same absolute value?

Yes, it is.


What Are The Differences Between Java And Python Variables Declaration?

You cannot declare a variable in Python without assigning some value to it. This is simply because a variable's type is inferred from the type of value that you assign to it. a = "hello world" # a is a string b = 42 # b is an integer c = 3.14 # c is a float d = [4, 8, 15, 16, 23, 42] # d is an integer array In Java, all variable declarations must explicitly include a type even when declared with an initial value. The initial value, when given, must be of the same type or of a type that is covariant with the variable's type. String a = "hello world"; int b = 42; double c = 3.14 int[] d = {4, 8, 15, 16, 23, 42} Note that local variables in Java that are declared but not assigned a value are considered "uninitialised". You must assign a value to all local variables before using them. Instance variables (members of a class) are always initialised. That is; numeric types have an initial value of zero, Boolean types are false and object references are null.


What are expressions that have the same value for all values of the variables?

Equivalent Expression