How to compare floating-point numbers in Scala

Scala FAQ: I need to compare two floating-point numbers in Scala, but as in some other programming languages, two floating-point numbers that should be equivalent may not be; how do I comparison floating-point numbers?


As in Java and many other languages, you solve this problem by creating a method that lets you specify the precision for your comparison. The following Scala “approximately equals” method demonstrates the approach:

Comparison of the 2015 Subaru Outback vs Forester alvin September 30, 2014 - 1:46pm

How to compare String equality in Java

Java String comparison FAQ: Can you share some examples of how to compare strings in Java?

If you’re like me, when I first started using Java, I wanted to use the == operator to test whether two String instances were equal, but that’s not the correct way to do it in Java.

In Scala you compare Strings with '==', not 'equals'

Scala String FAQ: How do you compare string equality in Scala?

In Scala you compare two Strings with the == operator. This is different than Java, where you compare two Strings with the equals method.

For example, given these strings:

A Java “instanceof null” example

You might think that when the Java instanceof operator is used to test a null reference, such as a null reference to a String, instanceof would realize that even though the reference is currently null, it is a reference to a String, so you might expect instanceof to return true ... but it doesn't.

A comparison of WordPress and Drupal (May, 2009)

Summary: A comparison of Wordpress and Drupal, circa 2009. (2011 Update: Drupal 7 is much easier to use than Drupal 6, which was described in this article.)

Perl if, else, elsif ("else if") syntax

Summary: This tutorial shows a collection of Perl if, else, and else if examples.

Here are some examples of the Perl if/else syntax, including the “else if” syntax, which is really elsif. (I wrote this because after working with many different languages I can never remember the “else if” syntax for most languages, and elsif is pretty rare.)

The Perl if/else syntax

The Perl if/else syntax is standard, I don’t have any problems here:

My first problem with Windows

In light of those recent Microsoft ads that show that Windows PCs are cheaper than Macs (without ever using the word "Windows"), here's my first problem with Microsoft Windows:

Three minutes and two seconds, how long it took to log into my Windows PC

Perl comparison operators

Perl equality FAQ: Can you share a list of the Perl equality operators?

Sure. Here's a convenient list of the Perl comparison operators (also known as Perl equality operators, equal, or not equal operators).

The Perl comparison operators are different for numeric and string comparison tests, as you can see in the following table: