Java math issue - eclipse

Can any one tell me how to write this type of code in eclipse propject. basically i want to square root the samv and samvi variables. i am getting syntax error.
int ki, l,;
int samv = (tdrum / k) / l;
int samvi = (tdrum / ki) / l;
int samv2 = (Math.sqrt(samv);
int samv2i = (Math.sqrt(samvi);

Many Error extra commas less brackets
int ki, l;
int samv = (tdrum / k) / l;
int samvi = (tdrum / ki) / l;
int samv2 = (Math.sqrt(samv));
int samv2i = (Math.sqrt(samvi));

Take care of the brackets. And use a double because sqrt() returns no int:
double samv2 = Math.sqrt(samv);
double samv2i = Math.sqrt(samvi);

Related

how to convert this result of calculation flutter

wanna know how to show this result like int number
var symbol = data[index]["symbol"];
double cureentprice = datarates["price"]["$symbol"];////this double is 1.17150
double enrty = double.parse(data[index]["entryprice"]);////this is 1.17100
double lo = cureentprice - enrty ;//i got like this result 0.00050
as you see above i got the result 0.00050 but i need it like this 50
any idea to do somthing like that??
Try This. Maybe This should Help. If this doesn't work then let me know.
var symbol = data[index]["symbol"];
double cureentprice = datarates["price"]["$symbol"];////this double is 1.17150
double enrty = double.parse(data[index]["entryprice"]);////this is 1.17100
// double lo = cureentprice - enrty ;//i got like this result 0.00050
int scale = 100000;
double lo = currentPrice - enrty;
var value1 = (lo * scale).floor();
var value2 = (lo * scale).ceil();
print(value1);
print(value2);
You can first scale and then round-off using floor() or ceil() function to get desired output.
double currentPrice = 1.17150;
double enrty = 1.17100;
int scale = 100000;
double lo = currentPrice - enrty;
print((lo * scale).floor()); //prints 49
print((lo * scale).ceil()); //prints 50

Flutter/Dart: Concatenat integers

Is it possible to concatenate integers without converting to String first?
int _test1 = 123;
int _test2 = 456;
print(int.parse(("$_test1"+"$_test2"))); // 123456
you can do it like this
void main() {
int _test1 = 123;
int _test2 = 456;
int pow = 10;
while(_test2 >= pow)
pow *= 10;
print(_test1 *pow + _test2);
}
source : How to concatenate two integers in C

How to count digits in BigDecimal?

I’m dealing with BigDecimal in Java and I need to make 2 check against BigDecimal fields in my DTO:
Number of digits of full part (before point) < 15
Total number of
digits < 32 including scale (zeros after point)
What is the best way to implement it? I extremely don’t want toBigInteger().toString() and .toString()
I think this will work.
BigDecimal d = new BigDecimal("921229392299229.2922929292920000");
int fractionCount = d.scale();
System.out.println(fractionCount);
int wholeCount = (int) (Math.ceil(Math.log10(d.longValue())));
System.out.println(wholeCount);
I did some testing of the above method vs using indexOf and subtracting lengths of strings. The above seems to be signficantly faster if my testing methodology is reasonable. Here is how I tested it.
Random r = new Random(29);
int nRuns = 1_000_000;
// create a list of 1 million BigDecimals
List<BigDecimal> testData = new ArrayList<>();
for (int j = 0; j < nRuns; j++) {
String wholePart = r.ints(r.nextInt(15) + 1, 0, 10).mapToObj(
String::valueOf).collect(Collectors.joining());
String fractionalPart = r.ints(r.nextInt(31) + 1, 0, 10).mapToObj(
String::valueOf).collect(Collectors.joining());
BigDecimal d = new BigDecimal(wholePart + "." + fractionalPart);
testData.add(d);
}
long start = System.nanoTime();
// Using math
for (BigDecimal d : testData) {
int fractionCount = d.scale();
int wholeCount = (int) (Math.ceil(Math.log10(d.longValue())));
}
long time = System.nanoTime() - start;
System.out.println(time / 1_000_000.);
start = System.nanoTime();
//Using strings
for (BigDecimal d : testData) {
String sd = d.toPlainString();
int n = sd.indexOf(".");
int m = sd.length() - n - 1;
}
time = System.nanoTime() - start;
System.out.println(time / 1_000_000.);
}

How to convert a binary number into a decimal fraction in dart?

Hi i have been wondering if there is a way in which to convert binary numbers into decimal fractions.
I know how to change base as an example through this code
String binary = "11110010";
//I'd like to change this line so it produces a decimal value
String denary = int.parse(binary, radix: 2).toRadixString(10);
If anyone still wondering how to convert decimal to binary and the inverse:
print(55.toRadixString(2)); // Outputs 110111
print(int.parse("110111", radix: 2)); Outputs 55
int binaryToDecimal(int n)
{
int num = n;
int dec_value = 0;
// Initializing base value to 1, i.e 2^0
int base = 1;
int temp = num;
while (temp) {
int last_digit = temp % 10;
temp = temp / 10;
dec_value += last_digit * base;
base = base * 2;
}
return dec_value;
}
int main()
{
int num = 10101001;
cout << binaryToDecimal(num) << endl;
}
This is my c++ solution but you can implement any language

iphone - converting float to integer

This is a simple question:
Is this a correct way to get an integer part from a float division?
int result = myFloat / anInteger;
this is working, but I am not sure if it is the best way.
thanks for any help.
To truncate:
int result = (int)(myFloat / anInteger);
Other conversion options:
#include <math.h>
int result = (int)ceilf(myFloat / anInteger);
int result = (int)roundf(myFloat / anInteger);
int result = (int)floorf(myFloat / anInteger);