What is the difference between = (single equal to) and == (double equal to)?

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

What is GEEK

Buddha Community

What is the difference between = (single equal to) and == (double equal to)?

What is the difference between = (single equal to) and == (double equal to)?

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

Joseph  Murray

Joseph Murray

1624097700

Double comparison in Java

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

Macey  Legros

Macey Legros

1599641040

Java String Equals Example | String equals() In Java

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 Example

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.

  1. Java String equals() method overrides an Object class equals() method implementation.
  2. Java String is immutable, checking the equality of string to another object should be done using equals() method rather than == operator.
  3. String equals() method always returns a boolean value; it doesn’t throw any exceptions.
  4. The result of the equals() method is true if and only if an argument is not null, it’s the String object, represents the same sequence of characters as this string.

See the following syntax.

string1.equals(string2);

#java #java string equals #equals

Paresh Sagar

1581940975

What Is A Single-Page Application?

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

Double VS Float in C++ – The Difference Between Floats and Doubles

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.

Difference Between Floats and Doubles

This section will be divided into sub-sections with each section focusing on one difference between floats and doubles.

Difference in Byte Size

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

}

Difference in Precision (Accuracy)

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.

Difference in Usage

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.

Conclusion

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/

#cpp #cplusplus #double