1601661070
What is the difference between = (single equal to) and == (double equal to)?
The = (single equal to) is use to assign the value on the right to a variable
on the left.
The == (double equal to) is use to compare to two values.
Explained By DDSRY
#difference between #singe equal to #double equal to #python #programming #developer
1601661070
What is the difference between = (single equal to) and == (double equal to)?
The = (single equal to) is use to assign the value on the right to a variable
on the left.
The == (double equal to) is use to compare to two values.
Explained By DDSRY
#difference between #singe equal to #double equal to #python #programming #developer
1624097700
Recently I was solving an interesting bug that came down to comparing two Double
variables with equals
method. It looks innocent, what can be wrong with something like firstDouble.equals(secondDouble)
?
The problem here is with how doubles are stored. To fit them into 64bytes (usually) they are rounded.
See the example below:
Double firstDouble = 0d;
for (int i = 1; i <= 42; i++) {
firstDouble += 0.1;
}
Double secondDouble = 0.1 * 42;
System.out.println(firstDouble); // 4.200000000000001
System.out.println(secondDouble); // 4.2
System.out.println(firstDouble.equals(secondDouble)); // false
This inaccuracy is caused by rounding errors.
We need to use a different approach to compare those doubles.
#java #double comparison in java #double comparison #comparisons #double
1599641040
In Java, objects of strings are immutable, so that means we cannot once the string is created. Now, if we have two strings and we want to compare them if they are the same or not, then we can use the equals()method.
Java String equals() method compares the two given strings based on the content of a string.
If any character is not matched, then it returns false and if all characters are matched, it returns true.
See the following syntax.
string1.equals(string2);
#java #java string equals #equals
1581940975
Single page web apps are an ideal choice when thinking about future web development. This architecture is a perfect choice for social networks stuff, SaaS platforms, or some close communities where SEO doesn’t matter.
#what is a single page application #single page application #single page application development
1653102900
In C++, there are various data types like string
, int
, char
, bool
, float
, and double
. Each of these data types have specific values that can be stored in them.
When working with integers, we usually store them in an int
data type. But this is only useful for whole numbers.
When we want to store numbers with decimals, we can either use the float
or double
. Though these two data types are used for a similar purpose, they have some differences.
In this article, we'll talk about the differences between floats and doubles in C++ along with some examples.
This section will be divided into sub-sections with each section focusing on one difference between floats and doubles.
The byte size for float
is 4 while the byte size for double
is 8.
This implies that double
can store values that are twice the amount that float
can hold.
We can see this by using the sizeof()
operator. Here is an example:
#include <iostream>
using namespace std;
int main() {
cout << "float: " << sizeof(float) << endl; // float: 4
cout << "double: " << sizeof(double) << endl;// double: 8
}
When working with numbers that have a lot of decimal digits, we usually hope that the resulting value will be accurate. But the accuracy of our result is dependent on the number of decimal digits we are dealing with.
Don't worry, we're still talking about C++, not mathematics.
float
and double
both have varying capacities when it comes to the number of decimal digits they can hold. float
can hold up to 7 decimal digits accurately while double
can hold up to 15.
Let's see some examples to demonstrate this.
#include <iomanip>
#include <iostream>
using namespace std;
int main() {
double MY_DOUBLE_VALUE = 5.12345678987;
float MY_FLOAT_VALUE = 5.12345678987;
cout << setprecision(7);
cout << MY_DOUBLE_VALUE << endl; // 5.123457
cout << MY_FLOAT_VALUE << endl; // 5.123457
}
In the example above, we created float
and double
variables – both having the same value: 5.12345678987
.
The setprecision()
function is used to tell the compiler the number of decimal places we want printed out. In our case, the value is 7.
We can observe from the results in the code above, that both variables printed accurate values up to the 7th decimal place: 5.123457
.
Let's increase the parameter in the setprecision()
function to 12 and see what happens.
#include <iomanip>
#include <iostream>
using namespace std;
int main() {
double MY_DOUBLE_VALUE = 5.12345678987;
float MY_FLOAT_VALUE = 5.12345678987;
cout << setprecision(12);
cout << MY_DOUBLE_VALUE << endl; // 5.12345678987
cout << MY_FLOAT_VALUE << endl; // 5.12345695496
}
From the results above, the MY_DOUBLE_VALUE
variable printed out accurate values. But the MY_FLOAT_VALUE
variable, from its 7th decimal place, printed out values entirely different from the original value it was given.
This shows us the precision of both data types. Just like float
, if we try to return a value that exceeds the accuracy range for the double
data type, we will get an inaccurate value returned.
float
is mostly used in graphic libraries for high processing power due to its small range.
double
is mostly used for calculations in programming to eliminate errors when decimal values are being rounded off. Although float
can still be used, it should only be in cases when we're dealing with small decimal values. To be on the safe side, you should always use double
.
In this article, we talked about the differences between floats and doubles in C++.
We talked about three differences: byte size, precision, and usage.
We also learned that doubles have twice the byte size of floats. Also, doubles are more accurate when dealing with large decimal values.
Lastly, we talked about use cases which helped us understand when to use each data type.
Happy coding!
Source: https://www.freecodecamp.org/news/double-vs-float-in-cpp-the-difference-between-floats-and-doubles/