Magic Behind String to Double

Magic Behind String to Double

by

Ahmad Fayyas

Magic Behind String to Double

by

Ahmad Fayyas

Although “String to Double” is one of the most popular obvious tasks, it could lead to funny magical stories!

 

What’s the story?

Once upon a time, I had a sub-task to generate doubles from given strings.

 

The requirement was:

The value of the given string should be only one word, a car brand or a number. Any whitespace should be trimmed if exists.

 

 

Magic Behind String to Double

What a great logic?! Can we deal with two different separated pieces instead?!

 

 

It’s still confusing me to this day! Nevertheless, I had to deal with it “as it was” because it was “urgent and we don’t have time to fix it from the server-side”.

Whatever… I implemented it as the following logic:

Straightforwardly, I converted the given trimmed string to a double, as
 

func doSomething(with myString: String) {
    guard let toDouble = Double(myString.trimmingCharacters(in: .whitespaces)) else {
        // cannot be converted, let's log something
        print("\(#function): double cannot be converted")
        return
    }
    // valid double, let's log its value
    print(toDouble)
}

// USAGE:
let string1ToConvert = "  -21.371212  "
doSomething(with: string1ToConvert) // -21.371212
let string2ToConvert = "123.00"
doSomething(with: string2ToConvert) // 123.0
let string3ToConvert = "Opel "
doSomething(with: string3ToConvert) // doSomething(with:): double cannot be converted

 

Double(myString.trimmingCharacters(in: .whitespaces))
 

and everything worked as expected.
 

 

Until MAGIC Happened ✨
 

After a while, I was informed that there was a defect in the area of the task that I resolved (the one that contains the conversion logic). I was curious to know what it was, so I decided to follow the fastest way, which was reviewing it directly with the “data entry sorceress” 🧙 so she could do her spell 💫 in order to reproduce the defect.

 

Tech Me

 


Thankfully, she was so cute 🌹 (I didn’t expect that to be honest), which made me insist on resolving the defect, before even knowing what it was.

After doing some “professional” negotiations with her, I discovered that the unexpected behavior occurred when inserting a car brand, I figured out that the input was “Infinity”.

 

And here is the “lol” part

I said

 

Honestly, I have no idea how you managed to do that, but the brand name is “Infiniti” not “Infinity”!

 

and naturally, I tried to explain that it was a bug and we would handle it ASAP. Suddenly, she blushed and she started to apologize! She even tried to make excuses that she “had to select the brand name from the given list instead of manually typing it”, thinking that her typo mistake was the main reason for the defect. Although I was about to roll on the floor laughing, I said

 

no worries 🙂

 

and I forgave her! Gentle Engineers!

 

However, it’s still a bug 🐞

After I had the new spell in hand, I got back to my laptop to learn more about it. The output of doSomething(with: “Infinity”) is inf, it’s the same as:

 

Double.infinity

 

We do know from our schools that infinity (∞) is a valid mathematical representation, it means something without any bound. However, that’s not what we usually expect (at least for my case or any similar one), we don’t care about these mathematical representations, we need to get an “actual” number!

So, I started to experiment with other strings that could be considered as valid doubles. I even attempted to pass other strings such as, symbols and emojis, to make sure of it. One of the things that I did which led me to good results, was passing the names of all static variables in the Double, therefore, I figured out that converting:
 

  • “nan”
  • “-nan”
  • “inf”
  • “-inf”
  • “infinity”
  • “-infinity”
     

gives a valid double value.

 

Wait… What?!

I thought it was clever to do my experiments on my own in this case, until the idea of:

It’s about how the double initializer recognizes the given string…


came to my mind, and I have no idea why it came so late! So, I reviewed the wanted initializer’s documentation, and… TADADADA!!!

The surprise was that I found ALL my “clever experiments” already mentioned on there, and not only that, but even more cases that I didn’t even think of… My experiments were just gone with the wind 😨.
 

Magic Behind String to Double
What an Ending!

To Sum Up

Here are of the main lessons we can learn from this story:
 

  • The obvious one is: in some rare cases, converting a string which doesn’t even contain a single number to a double could give a valid result.
  • Doing coding experiments is cool, it is one of the reasons why a developer understands how things work more than the others, but not before reviewing the documentation, you might end up with meaningless experiments.
  • Even if you think of it as “too obvious”, I’d encourage you to still review the documentation. If you never did that before, you might see something new.
  • Don’t be frustrated while working on a task coming from a “really weird” requirement, it might give you the chance to meet cute people and learn new spells!

Thanks for Reading!