-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8d5ce99
commit 2938fde
Showing
1 changed file
with
48 additions
and
0 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
...rums/jeorg-kotlin-crums-4-variance/src/main/kotlin/org/jesperancinha/ktd/FloatVsDouble.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package org.jesperancinha.ktd | ||
|
||
class FloatVsDouble { | ||
companion object { | ||
@JvmStatic | ||
fun main(args: Array<String>) { | ||
val floatNumber = 7.3333333333333333333333333333333f | ||
println(floatNumber) | ||
println("The number $floatNumber has ${floatNumber.toString().length - 1} digits in precision") | ||
val doubleNumber = 7.3333333333333333333333333333333 | ||
println(doubleNumber) | ||
println("The number $doubleNumber has ${doubleNumber.toString().length - 1} digits in precision") | ||
val bigDecimal = "7.3333333333333333333333333333333".toBigDecimal() | ||
println("The number $bigDecimal has ${bigDecimal.toString().length - 1} digits in precision") | ||
|
||
val value1 = 9 | ||
val value2 = 9 | ||
|
||
println("This is the result of a bad idea sum ${badIdeaSum(value1, value2)}") | ||
println("This is the result of a good idea sum ${goodIdeaSum(value1, value2)}") | ||
} | ||
|
||
/** | ||
* This function will try to sum | ||
* value1,000,000,000,000,000,000 | ||
* with | ||
* value2,000,000,000,000,000,000 | ||
* correctly | ||
*/ | ||
private fun goodIdeaSum(value1: Int, value2: Int): String { | ||
return ((value1 + value2).toBigDecimal().multiply(1_000_000_000_000_000_000.toBigDecimal() )).toString() | ||
} | ||
|
||
/** | ||
* This function will try to sum | ||
* value1,000,000,000,000,000,000 | ||
* with | ||
* value2,000,000,000,000,000,000 | ||
* using a terrible idea | ||
* NOTE that this idea is defended by some interviewers in selection processes. | ||
* Do not feel bad about it if the interviewer rejects your application because of this. | ||
* You probably wouldn't want to work in a place with these criteria any way. | ||
*/ | ||
private fun badIdeaSum(value1: Int, value2: Int): String { | ||
return ((value1 + value2) * 1_000_000_000_000_000_000).toString() | ||
} | ||
} | ||
} |