Convert Double To Long With Checks - double

As you can see by the code below it should if out of range return the right long value without getting a random number when casting. However this code doesn't execute for the minimum value at all?
System.out.println(Double.MIN_VALUE < Long.MAX_VALUE);//this prints true so it should at the min value be in range meaning normal casting should work
public static long castLong(double d)
{
if(d > Long.MAX_VALUE)
return Long.MAX_VALUE;
else if(d < Long.MIN_VALUE)
{
System.out.println("here");
return Long.MIN_VALUE;
}
return (long)d;
}
based on the code above it should cast the issue is even when the values are in range it doesn't cast properly just goes to 0. So What am I suppose to do for said function make a double/float to string then how do I convert the string into long since it prints "4.9E-324". So what for the E fill with zeros? or what am I suppose to do to convert it in range of long max/min value when converting?

Related

Issue with Double datatype in Scala

New to Scala and am trying to come up with a library in Scala to check if the double value being passed is of a certain precision and scale. What I noticed was that if the value being passed is 1.00001 then I get the value as that in my called function, but if the value being passed is 0.00001 then I get the value as 1.0E-5, Is there any way to preserve the number in Scala?
def checkPrecisionAndScaleFormat(precision: Int, scale: Int)(valueToCheck: Double): Boolean = {
val value = BigDecimal(valueToCheck)
value.precision <= precision && value.scale <= scale
}
What I noticed was that if the value being passed is 1.00001 then I get the value as that in my called function, but if the value being passed is 0.00001 then I get the value as 1.0E-5
From your phrasing, it seems like you see 1.00001 and 1.0E-5 when debugging (either by printing or in the debugger). It's important to understand that
this doesn't correspond to any internal difference, it's just how Double.toString is defined in Java.
when you do something like val x = 1.00001, the value isn't exactly 1.00001 but the closest number representable as a Double: 1.000010000000000065512040237081237137317657470703125. The easiest way to see the exact value is actually looking at BigDecimal.exact(valueToCheck).
The only way to preserve the number is not to work with Double to begin with. If it's passed as a string, create the BigDecimal from the string. If it's the result of some calculations as a double, consider doing them on BigDecimals instead. But string representation of a Double simply doesn't carry the information you want.

Number validation and formatting

I want to format, in real time, the number entered into a UITextField. Depending on the field, the number may be an integer or a double, may be positive or negative.
Integers are easy (see below).
Doubles should be displayed exactly as the user enters with three possible exceptions:
If the user begins with a decimal separator, or a negative sign followed by a decimal separator, insert a leading zero:
"." becomes "0."
"-." becomes "-0."
Remove any "excess" leading zeros if the user deletes a decimal point:
If the number is "0.00023" and the decimal point is deleted, the number should become "23".
Do not allow a leading zero if the next character is not a decimal separator:
"03" becomes "3".
Long story short, one and only one leading zero, no trailing zeros.
It seemed like the easiest idea was to convert the (already validated) string to a number then use format specifiers. I've scoured:
https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html
and
http://www.cplusplus.com/reference/cstdio/printf/
and others but can't figure out how to format a double so that it does not add a decimal when there are no digits after it, or any trailing zeros. For example:
x = 23.0
print (String(format: "%f", x))
//output is 23.000000
//I want 23
x = 23.45
print (String(format: "%f", x))
//output is 23.450000
//I want 23.45
On How to create a string with format?, I found this gem:
var str = "\(INT_VALUE) , \(FLOAT_VALUE) , \(DOUBLE_VALUE), \(STRING_VALUE)"
print(str)
It works perfectly for integers (why I said integers are easy above), but for doubles it appends a ".0" onto the first character the user enters. (It does work perfectly in Playground, but not my program (why???).
Will I have to resort to counting the number of digits before and after the decimal separator and inserting them into a format specifier? (And if so, how do I count those? I know how to create the format specifier.) Or is there a really simple way or a quick fix to use that one-liner above?
Thanks!
Turned out to be simple without using NumberFormatter (which I'm not so sure would really have accomplished what I want without a LOT more work).
let decimalSeparator = NSLocale.current.decimalSeparator! as String
var tempStr: String = textField.text
var i: Int = tempStr.count
//remove leading zeros for positive numbers (integer or real)
if i > 1 {
while (tempStr[0] == "0" && tempStr[1] != decimalSeparator[0] ) {
tempStr.remove(at: tempStr.startIndex)
i = i - 1
if i < 2 {
break
}
}
}
//remove leading zeros for negative numbers (integer or real)
if i > 2 {
while (tempStr[0] == "-" && tempStr[1] == "0") && tempStr[2] != decimalSeparator[0] {
tempStr.remove(at: tempStr.index(tempStr.startIndex, offsetBy: 1))
i = i - 1
if i < 3 {
break
}
}
}
Using the following extension to subscript the string:
extension String {
subscript (i: Int) -> Character {
return self[index(startIndex, offsetBy: i)]
}
}

how Two Double values equality always coming false even if the values are equal in Swift?

Hi Here I'm comparing to double values equal or not. but its returing always false even if both Double values are Equal.
let latestlogoValue = log(Double(125))/log(5.0)
let latestlogIntValue:Int = Int(latestlogoValue)
print(latestlogoValue)
print(Double(latestlogIntValue))
print(Double(latestlogIntValue) == Double(latestlogoValue)) //Always returning false
Double or float value comparison with == sign, will not give you the exact answer. You may think that the two values are equal to each other but they are slightly different. Doubles are stored differently in memory. You can test it by printing as String like below-
print(String(format: "%.20f", Double(latestlogoValue))) //3.00000000000000044409
print(String(format: "%.20f", Double(latestlogIntValue))) //3.00000000000000000000
So you can update your comparison function as
func isDoubleEqual(_ first: Double, _ second: Double) -> Bool {
return fabs(first - second) < Double.ulpOfOne
}
That is happened because of Double type's precision
If we print your values
print(String(format: "a float number: %.55f", latestlogoValue))
print(String(format: "a float number: %.55f", Double(latestlogIntValue)))
we'll see difference in values:
a float number: 3.00000000000000044408920985006261616945266723632812500
a float number: 3.00000000000000000000000000000000000000000000000000000
So the values are different, if you need to compare float or double values use comparing them with some precision
Look at the precesion values are different they are not identical.
Just print them out.
print(Double(latestlogoValue).debugDescription) // 3.0000000000000004
print(Double(latestlogIntValue).debugDescription) // 3.0
And you are comparing the same which results always false.
print(Double(latestlogIntValue) == Double(latestlogoValue))
// 3.0000000000000004 == 3.0 results false obvious
let latestlogoValue = log(Double(125))/log(5.0)
let latestlogIntValue:Int = Int(latestlogoValue)
print(latestlogoValue)
print(Double(latestlogIntValue))
print(Double(latestlogIntValue) == Double(latestlogoValue))
Here Double(latestlogIntValue) & Double(latestlogoValue)) are getting different values . Compare the values with Int you will get true
print(Int(latestlogIntValue) == Int(latestlogoValue)) // true

SSRS Expression works as cell value expression, but not as background color value expression

I have an SSRS report with a matrix in it, where I needed to display the Growth Percentage in a column group compared to the previous column value. I managed this by using custom code...
DIM PreviousColValue AS Decimal
Dim RowName AS String = ""
Public Function GetPreviousColValue(byval Val as Decimal, byval rwName as string) as Decimal
DIM Local_PreviousColValue AS Decimal
IF RowName <> rwName THEN
RowName = rwName
PreviousColValue = val
Local_PreviousColValue = 0
ELSE
Local_PreviousColValue = (Val - PreviousColValue)/PreviousColValue
PreviousColValue = val
END IF
Return Local_PreviousColValue
End Function
..and then using this as the value expression in the cell..
=Round(Code.GetPreviousColValue(ReportItems!Textbox8.Value,Fields!BusinessUnit.Value)*100,0,system.MidpointRounding.AwayFromZero)
So far so good, this produces the expected value. Now I need to use this expression in a background color expression to get a red/yellow/green but in that capacity it fails.
The background color expression looks like this: =IIF(ROUND(Code.GetPreviousColValue(ReportItems!Textbox9.Value,Fields!Salesperson.Value)*100,0,System.MidpointRounding.AwayFromZero)<=-5,"Red"
,IIF(ROUND(Code.GetPreviousColValue(ReportItems!Textbox9.Value,Fields!Salesperson.Value)*100,0,System.MidpointRounding.AwayFromZero) >=5,"Green"
,"Yellow"))
When I run the report the background color expression only ever returns yellow. As a test I pasted the background color expression in as the cell value and ran it again. Results in the image below
I get no build or run time errors so I'm not sure why this does not work.
After some more searching I found a better Custom Code solution than what I was using to get the Growth Percentage in a column group compared to the previous column value. Besides being simpler to read this version has an added benefit: You can dynamically hide the growth percentage column for your first instance of the column group (because it will always be zero or null) and still get the right values in the 2nd/3rd/4th instance of the column group.
Public Function GetDeltaPercentage(ByVal PreviousValue, ByVal CurrentValue) As Object
If IsNothing(PreviousValue) OR IsNothing(CurrentValue) Then
Return Nothing
Else if PreviousValue = 0 OR CurrentValue = 0 Then
Return Nothing
Else
Return (CurrentValue - PreviousValue) / PreviousValue
End If
End Function
The new function is called like so
=Code.GetDeltaPercentage(Previous(Sum(<expression or dataset field>),"Group ByColumn"), Sum(<expression or dataset field>))
Re: the original question - why does my cell value expression not work when used as the background color expression - I took an easy out and just referenced the cell value.
=IIF(ROUND(Me.Value*100,0,System.MidpointRounding.AwayFromZero)<=-5,"Red"
,IIF(ROUND(Me.Value*100,0,System.MidpointRounding.AwayFromZero) >=5,"Green"
,"Yellow"))

Fast Fibonacci using recursion

I implemented a Fibonacci function in Scala and it works fine however when I enter 50 it takes a long time to compute it because it has to calculate the 2 previous integers each time. I found a function that keeps the 2 previous numbers. However, can somebody tell me how to write this function to make it accept 2 integers instead of 3 and return the last 2 numbers to compute the Fibonacci at a particular index x. Thanks!
def fastFib(x: Long ): Long = {
def fast(x:Long , a:Long, b:Long):Long =
if (x<=0) a+b
else fast(x-1,b,a+b)
if (x<2) 1
else fast(x-2,0,1)
}
You can cache the intermediate results, then you never recompute the same result twice
Here is the code
//this supposed to contains all the value when initialized
//initialized with 0 for all value
val cache = Array [Int] (101);//0 to 100
cache(1)==1;//initial value
cache(2)=1;//initial value
def fibonacciCache(n:Int) : Int = {
if (n>100)
{
println("error");
return -1;
}
if (cache(n)!=0)//means value has been calculated
return cache(n);
else
{
cache(n)=fibonacciCache(n-1)+fibonacciCache(n-2);
return cache(n);
}
}
Hope that helps