I am trying to write a program in C to get the percent of even numbers in an array. I am thinking of writing it using int datatype. But some one mentioned to me using double will be easier. I don't understand that. Can anyone guide me with it?
What does double datatype return?
Can the return statement be given as return (double)? What will that give?
Can double convert a real number to a percent? Eg: 0.5 to 50.0
The int datatype is, as the name would suggest, integers (or whole numbers). So you cannot represent a decimal like 0.5. A double is decimal number. So you can hold numbers like 0.5. Common practice is to store your percentage as a simple decimal number like 0.5 (using the double type). Then when you need to display nicely as 50.0%, just multiply by 100 before displaying.
Here's a useful link on C's basic datatypes: http://www.tutorialspoint.com/ansi_c/c_basic_datatypes.htm
Related
I am trying to convert a double value to int and use it in the number of agents per arrival. I have a distribution but want it to round to the nearest integer. I looked up the anylogic math functions but the only one that makes sense is rint but that still returns a double.
you can do this:
(int) rint(yourVariable)
Another way is to use (int)floor(myDouble)) and (int)ceil(myDouble)) .
With these, you can decide if you want to round up or down (if the default rounding rules aren't to your liking)
I have a float that we'd like to truncate the fractional part off of but not sure the easiest way to do this. Just picking up Swift and most of the thoughts seemed way too involved. Currently I have:
details.details.append((titles[2], "\(averageAnnualSolarProduction) kW"))
but this is giving me a fraction which I want removed.
We need to combine two things:
String's format: initializer
floor operation which will make sure our decimal number doesn't round up.
If we want to keep these operations in-line, we'd want something that looks like this:
details.details.append((titles[2], String(format: "%.0f kW", floor(averageAnnualSolarProduction))))
But that's a lot of parenthesis. Looks a little better if we unnest this a bit.
let solarProduction = String(format: "%.0f kW", floor(averageAnnualSolarProduction))
details.details.append((titles[2], solarProduction))
For clarity here, floor takes a number like 3.89 and returns the largest integer number smaller than what was passed in, so it would return 3.0 here.
And to be sure that we're not printing the .0, we use String's format: initializer, which takes a format string and arguments. This works just like format strings have worked since at least C. %f specifies our argument is a floating point number, and the .0 specifies that we will display zero numbers after the decimal point.
If we're find with rounding up, we can drop the call to floor and simply use the format: initializer, but for a value of 3.89, this would give us a string that looks like "4 kW".
Here is my code:
println(Double(2/5))
When I run this, it prints out
0.0
How can I fix this? I want it to come out to 0.4. It there some issue with the rounding?
The problem is that you're not converting to a Double until after you've done integer division between two integers. Let's take a look at order of operations. We start at the inside and move outward.
Perform integer division between the integer 2 and the integer 5, which results in the integer 0.
Create a double from the integer 0, which creates the double 0.0.
Call description on the double 0.0, which returns the string "0.0"
Call println on the string "0.0"
We can fix this by calling the Double constructor on each side of the division before we divide them.
println((Double(2)/Double(5)))
Now the order of operations is:
Convert the integer 2 to the floating point 2.0
Convert the integer 5 to the floating point 5.0
Perform floating point division between these floating point numbers, resulting in 0.4
Call description on the floating point number 0.4, which returns the string "0.4".
Call println on the string "0.4".
Note that it's not strictly necessary to convert both sides of the division to Double.
And as long as we're dealing with literals, we can just write println(2.0/5.0).
We could also get away with writing println((2 * 1.0)/5) which should now interpret all of our literals as floating point (as we've multiplied it by a floating point).
As long as either side of a math operating is a floating point type, the integer literal will be interpreted as a floating point type by Swift, but in my opinion, it's far better to explicitly convert our types so that we're excruciatingly clear on exactly what we want to happen. So let's get all of our numbers into the same type and be explicitly clear what we actually want.
If we're dealing with literals, we can add .0 to them to force them as floating point numbers:
println(2.0/5.0)
If we're doing with variables, we can use a constructor:
let myTwoInt: Int = 2
let myFiveInt: Int = 5
println((Double(myTwoInt)/Double(myFiveInt))
I think your issue is that you are dividing two integers which normally will return an integer.
I had a similar issue in java, adding a .0 to one or the other integers or converting either to a double by using the double function should fix it.
It's a feature of typed languages that creates a result of the same type as the values being divided.
Digits is correct about the cause; instead of the approach you're taking, try this:
print(2.0 / 5.0)
I want to use xor for my double numbers in matlab,but bitxor is only working for int numbers. Is there a function that could convert double to int in Matlab?
The functions You are looking for might be: int8(number), int16(number), uint32(number) Any of them will convert Double to an Integer, but You must pick the best one for the result You want to achieve. Remember that You cannot cast from Double to Integer without rounding the number.
If I understood You correcly, You could create a function that would simply remove the "comma" from the Double number by multiplying your starting value by 2^n and then casting it to Integer using any of the functions mentioned earlier, performing whatever you want and then returning comma to its original position by dividing the number by 2^n
Multiplying the starting value by 2^n is a hack that will decrease the rounding error.
The perfect value for n would be the number of digits after the comma if this number is relatively small.
Please also specify, why are You trying to do this? This doesn't seem to be the optimal solution.
You can just cast to an integer:
a = 1.003
int8(a)
ans =
1
That gives you an 8 bit signed integer, you can also get other size i.e. int16 or else unsigned i.e. uint8 depending on what you want to do
I have an issue with rounding the result of a calculation to two decimal places.
It is a financial calculation and when the result involves half a penny I would expect the number to be rounded up but it is in fact being rounded down.
To replicate the issue:
float raw = 16.695;
NSLog(#"All DP: %f",raw);
NSLog(#"2 DP: %.2f",raw);
Returns:
All DP: 16.695000
2 DP: 16.69
Whereas I would expect to see:
All DP: 16.695000
2 DP: 16.70
Can anyone advise if this is by design or if (most likely) I am missing something and if there is anything I can do to get around it. It is vital that it rounds up in this scenario.
Thanks in advance,
Oli
Don't use floats for financial calculations!
There are values that floats cannot represent exactly. 16.695 is one of them. When you try to store that value in a float, you actually get the closest representable value. When you perform a series of operations on a float, you can lose precision, and then you lose accuracy. This leads to losing money.
Use an actual currency type, use NSDecimalNumber, or do all your calculations with ints that count the smallest unit you care about (i.e., 1050 is $10.50 in whole cents, or $1.050 if you want fractions of pennies).
As far as I am aware the NSLog() function only takes formatting arguments and makes no attempt to round.
You may be use to using a printf() style function that does support rounding.
I suggest using one of the many functions in math.h to round your value before output and only rely on NSLog() for formatting.
After seeing the comments
Use the C standard function family round(). roundf() for float, round() for double, and roundl() for long double. You can then cast the result to the integer type of your choice
you could try this:
- (void)testNSlogMethod {
float value = 16.695; //--16.70
value = value *100;
int mulitpler = round(value);
NSLog(#"%.2f",mulitpler/100.0f);
}