Type conversion to double value in java [closed] - scjp

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 7 years ago.
Improve this question
public class TypeConversion4 {
public static void main(String[] args) {
double d = 2D + 2d + 2. + 2l + 2L + 2f + 2F + 2.f + 2.D;
System.out.println(d); //prints 18.0
}
}
how it prints 18.0. Can anyone provide some analysis.

So, what's the problem? All these twos converted to the biggest type while summing up and then the result casted to double. But you can store 2 in long, int, double and float without any error. That means that all you have to do is to sum these 9 twos and come up with 18.0.

Related

Haven´t found how to convert a num to a float [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
i am trying to convert a num toFloat but i dont find anything useful at the moment,
Does anyone know how to do it?
I am using this to obtain the number in the string but i don´t find how to convert it to float
val str = "awdafafaf123asfasf"
val num = str.replace(Regex("[^0-9]"),"")
println(num)
}
You can use String.to*() category of functions to convert a string to various other data types.For example:
val str = "awdafafaf123asfasf"
val num = str.replace(Regex("[^0-9]"),"")
val intValue: Int = num.toInt()
val doubleValue: Double = num.toDouble()
val floatValue: Float = num.toFloat()
println(num)
Kotlin standard library also provides String.to*OrNull() versions of these functions which return null if conversion is not possible.

Bit operator AND does not return a value [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
So I have this code:
Dim a As Byte = (1 << 1)
Dim b As Byte = (1 << 2)
Dim c As Byte = (1 << 3)
Dim res As Byte
res = a And b And c
The result (res) is 0. Why? I just want to manipulate the bits in this, but the return value is always 0. All tutorials show me, that this should work. Some do something like Dim a As Byte = 2, but shouldn't it be unimportant how to set the value of a byte?
My debugger also shows me that the bytes a, b and c are set correctly. What am I doing wrong?
Edit:
It was a huge mistake on my side. I knew that I needed to use the OR operator but didnt. I just had a big brain lag. Excuse me for being this impatient and not thinking this over properly. I will leave this question here so maybe someone sees that this is his/her problem, too.
Again, please excuse me.
Because the result of 2 and 4 and 8 is 0. None of those numbers have any binary columnar digits in common:
2 is 0010
4 is 0100
8 is 1000
AND works down the column and only records a 1 in the result if all the values in the column are 1:
3 AND 5
3 is 0011
5 is 0101
result is 0001
^ only this column has all 1s in it.
The result of 3 And 5 is 1
All tutorials show me, that this should work
Show us those tutorials, so we can better understand what is going wrong in the process of the_tutorial --> your_brain - one or the other parts of that operation has an error! :)
I suspect that you don't actually understand what the bitwise And operator does. Zero is exactly what you should expect from that code. You should have told us this in the first place but what do you expect and why? I suspect that you actually want the Or operator rather than And. Regardless, you should read up and learn what those bitwise operators actually do. It might also help to look at those values in a more obvious way:
Module Module1
Sub Main()
Dim a As Byte = (1 << 1)
Dim b As Byte = (1 << 2)
Dim c As Byte = (1 << 3)
Display(a)
Display(b)
Display(c)
Display(a And b And c)
Display(a Or b Or c)
Console.ReadLine()
End Sub
Sub Display(b As Byte)
Console.WriteLine(Convert.ToString(b, 2).PadLeft(32, "0"c))
End Sub
End Module
Output:
00000000000000000000000000000010
00000000000000000000000000000100
00000000000000000000000000001000
00000000000000000000000000000000
00000000000000000000000000001110

Why Float error is different in C? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Please see the pictures:
I'm amazing the result is different.
A CGFloat is actually a double on 64 bit platforms. (It was a float on old 32 bit platforms.)
So here you're dividing a double by a double:
CGFloat price = 88888736 / a;
^^^^^ ^^^^^^^^ ^
double int -> double double
and here you're dividing a double by a float:
CGFloat price2 = 88888736 / 100.0f;
^^^^^^ ^^^^^^^^ ^^^^^^
double int -> double float
Change 100.0f to either 100.0 or (CGFloat)100 and you should be fine.
LIVE DEMO
CGFloat is a double on your machine, therefore what you are doing is this:
double a = 100.00f
double price = 88888736 / a
float a2 = 100.00f // `float` type enforced by the trailing `f`
double price2 = 88888736 / a2
The difference is that in the first case the division is a double division while in the second case this is a float division where the result is then assigned to a double. Since float division has less precision, you get the result you are seeing.

How do I set x = x + 1 for y amount of times without a for or do loop? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
In Vb I am trying to set variable x = x + 10 for y amounts of time.
What I have now is this:
For z As Double = 0 To Y
x = x + 1
Next
I want no do or for loops because they take up a lot of time and resources for larger y values.
This is a job for (cue dramatic music) Mathman!
I think you'll find multiplication is the key, something along the lines of
x = x + y * 10
if you want to add ten each time, or the even simpler
x = x + y
if you're only adding one.
Although you may have to use (y + 1) instead of y if your loop truly is 0 through y inclusive. It's a little unclear from the question exactly what you're after.

Percentage Plus and Minus [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
Having an issue calculating money after percentages have been added and then subtracting , I know that 5353.29 + 18% = 6316.88 which I need to do in my tsql but I also need to do the reverse and take 18% from 6316.88 to get back to 5353.29 all in tsql, I might have just been looking at this too long but I just cant get the figures to calculate properly, any help please?
newVal = 5353.29(1 + .18)
origVal = newVal/(1 + .18)
6316.88 is 118%, so to get back you need to divide 6316.88 by 118, then multiply by 100.
(6316.88/118)*100=5353.29
To Add n% to X
Result = X * ( 1 + (n / 100.0))
To do the reverse
X = Result / ( 1 + (n / 100.0))