Getting NaN when generating a percentage - double

So here is my code:
int var1 = serverList[i].webUp;
int var2 = serverList[i].webUp + serverList[i].webDown;
double ratio = (double)var1 / var2;
ratio = ratio * 100.0;
string percentage = string.Format("Up time is {0:P2}%", ratio);
Every time I run my application I keep getting NaN instead of a percent. webUp and webDown are both int, and I'm type casting to double when I do the division. Any ideas? I'm not dividing by 0, all values are greater than 0 and never in the negative. I keep getting "Up time is NaN%"

Related

Failed to multiply double in dart

I got an error when trying to multiply a number with a floating point in dart. Does anyone know why this happens?
void main() {
double x = 37.8;
int y = 100;
var z = x * y;
print(z);
// 3779.9999999999995
}
In other languages ​​(C#/C++) I would have the result: 3780.0
This is completely expected because 37.8 (or rather, the 0.8 part) cannot be precisely encoded as a binary fraction in the IEEE754 standard so instead you get a close approximation that will include an error term in the LSBs of the fraction.
If you need numbers that are lossless (e.g. if you are handling monetary calculations) then check out the decimal package.
A simpler hack if your floating point number has sufficient bits allocated to the fraction to keep the erroroneous bits out of the way is to round off the number after your calculation to the number of decimal places that you care about.
You can use toStringAsFixed to control fraction digits.
void main() {
double x = 37.8;
int y = 100;
var z = x * y;
print(z.toStringAsFixed(1));
// 3780.0
}

how to print the decimal value in iphone?

I have a double value as 47. I divide it by 60, but when I try to print it it shows 0.0000.
It should display 0.7858
How can I do that? Any help?
Here is my code:
int minutes = decimal * 60;
float min = (minutes)/60;
NSLog(#"%.4f",min);
Try this :
double decimal = 47.0 / 60.0; // Just so minutes is 47
double minutes = decimal * 60.0;
double min = minutes / 60.0;
NSLog(#"%.4f",min);
In Objective C (as well as C and C++), integer division is applied when both left and right operands are integers, so minutes / 60 = 0. If you want a floating point operation, use floating points literals.
try this:
int minutes = 40;
float min = (minutes * 1.0f)/60.0f;
NSLog(#"%.4f",min);
or
int minutes = 40;
float min = (float)minutes/60.0f;
NSLog(#"%.4f",min);

Display Float In UILabel

I am having an issue with using a float in a UILabel.
float doubleNum;
floatNum = 10 / 20;
cashLabel.text = [[NSString alloc] initWithFormat:#"%f", floatNum];
If I use "floatNum = 10 / 10;" it correctly returns "1.000000000", however, if I put in "floatNum = 10 / 20" it returns "0.0000000". I have tried about everything I know and it does not work. I know it's a dumb mistake, but I can't figure it out.
Happy Holidays. :)
You need to cast one of the integer's to a float.
Try replacing the divisional line with:
float floatNum = (float) 10 / 20;
and you should get the correct answer.
Or if possible just use floats in your division:
float floatNum = 10.0f / 20.0f;
should also work
The issue here is that you are assigning floatNum the result of dividing one INTEGER by another. The result of 10 / 20 is indeed 0 and as a float, it appears as 0.0000000. In order to obtain a proper result, you need to either use a cast type to turn it into a float or add a .0 to one of the numbers. In division, if one of the numbers is a float (which is easily done by just adding a .0 to one of them), the result will be a float as well.
Normally, C performs "integer division" (basically, division without the remainder -- 10/3 is 3R1, so it yields 3).
When you type floatNum = 10/20, it does 10/20 = 0 (remainder 10).
To fix this, you have to tell the program that you're giving it floating point numbers. Try using:
floatNum = 10.0 / 20,
floatNum = 10 / 20.0, or
floatNum = float (10 / 20).
All of those should work.
try floatNum = 10.0f/20.0f (i.e. make sure the calculation is being done with floats rather than ints)
Consider following example to understand how floats work:
float a = 1/120;
float b = 1.0/120;
float c = 1.0/120.0;
float d = 1.0f/120.0f;
NSLog(#"Value of A:%f B:%f C:%f D:%f",a,b,c,d);
Output: Value of A:0.000000 B:0.008333 C:0.008333 D:0.008333
For float variable a : int / int yields integer which you are assigning to float and printing it so 0.0000000
For float variable b: float / int yields float, assigning to float and printing it 0.008333
For float variable c: float / float yields float, so 0.008333
Last one is more precise float. Previous ones are of type double: all floating point values are stored as double data types unless the value is followed by an 'f' to specifically specify a float rather than as a double.
Change your code to:
float floatNum;
floatNum = 10.0f / 20.0f;
cashLabel.text = [[NSString alloc] initWithFormat:#"%f", floatNum];

work out percentage

I am using the following code to work out the percentages of two numbers, could some on please help me remove the decimal numbers off pcntNo and pcntYes
//Work out percentages
int yes = [currentYes intValue];
int no = [currentNo intValue];
int total = yes + no;
int pcntYes = (yes / total) * 100;
int pcntNo = (no / total) * 100;
It always returns 0
Also i want it with no decimal places if that is possible
Thanks
What's "decimal numbers"? If you mean fractional part, you won't have those - your percentages are stored as integers. By the way, you want slightly different arithmetics:
int pcntYes = (yes * 100) / total;
int pcntNo = (no *100) / total;
Otherwise, integer division will yield only zeros.
Use the floorf function from math.h:
int pcntYes = floorf((yes / total) * 100);

% operator for time calculation

I am trying to display minutes and seconds based on a number of seconds.
I have:
float seconds = 200;
float mins = seconds / 60.0;
float sec = mins % 60.0;
[timeIndexLabel setText:[NSString stringWithFormat:#"%.2f , %.2f", mins,seconds]];
But I get an error: invalid operands of types 'float' and 'double' to binary 'operator%'
And I don't understand why... Can someone throw me a bone!?
A lot of languages only define the % operator to work on integer operands. Try casting seconds and mins to int before you use % (or just declare them int in the first place). The constant values you use will also need to be int (use 60 instead of 60.0).
As others have pointed out, you should be using integers. However, noone seems to have spotted that the result will be incorrect. Go back and have another look at modulo arithmetic, and you'll realize you should be doing
int seconds = 200;
int mins = seconds / 60;
int sec = seconds % 60;
Note the last line, seconds % 60 rather than mins % 60 (which will return the remainder of the minutes divided by 60, which is the number of minutes to the hour, and completely unrelated to this calculation).
EDIT
doh, forgot the ints... :)
The 60.0 forces a conversion to double
try:
float seconds = 200;
float mins = seconds / 60;
float sec = mins % 60;
Use ints instead. At least in your example, seems like they're enough (it will also be faster and clearer).
Also, in this case you would get 3.3333... mins, and not 3 minutes as expected. You could use Math.ceil(x) if you need to work with floats.
Do like this:
float seconds = 200.5;
float mins = floor(seconds / 60.0);
float sec = seconds - mins * 60.0;