Why is 500/1000 is printing 0? [duplicate] - swift

This question already has answers here:
Math divison in Swift
(4 answers)
Is the Swift divide "/" operator not working or have I missed something?
(3 answers)
Closed 1 year ago.
I'm trying to understand why print(500/1000) print 0 ?
It should be 0.5 but it's not printing it?!
This happed to me when I started to show a progress bar based on total value and current value but I came across this issue and now I'm stuck

You are working with whole numbers, so it's returning a whole number.
If you use decimals, it should divide as expected.

I am not really in Swift, but I assume that in this language 500 and 1000 are both integer values (like in other programming languages).
Then 500/1000 is 0, with a remainder of 500.
If you want to have a fractional result, you should consider to use the proper datatype (I don't know what this is in Swift ... something like double or float, I assume): write 500.0/1000.0.

Related

Can't convert and use string value in Double - Swift [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 4 years ago.
I have a easy problem but I can't fix it. In my program There are lines where I have a for loop with custom array. This array has got 3 value each of them string(array created by the JSON object from web). Whenever I tried the reach these string variables in double form I can't as pictures show.
As you can see in the pictures my aim is get he latitude and longitude values turn into double and use in CLLocationCoordinate2D init. but the conversion of string value is for latitude = 35.32041000000002 how can I convert it into presicion 6 digit. Thanks all. Have a nice day.
Read this to understand floating point numbers. In summary: Not all numbers can be exactly represented in the 64-bits of a Double. So you'll end up with something that extremely close, but with extra digit(s), or for example with a long tail of 9999999999.
The conversion succeeded. The value that the extra digit (0.000000000000002) won't hurt as they are rather insignificant.

why ncread function in matlab reduce floating point numbers count? [duplicate]

This question already has answers here:
How to store more than 4 decimal places in an array in MATLAB
(2 answers)
Closed 5 years ago.
i have code below in matlab:
converted = ncread(this_file, 'U');
disp(converted(50,10,20));
and the result is:
-0.1561
actually the number is -0.15617890 but this code changes the the number of floating numbers. why?
MATLAB displays only 4 digits after the decimal point by default. You can use format to display more digits:
format long
converted = ncread(this_file, 'U');
disp(converted(50,10,20));

Swift : impossible to round a double correctly [duplicate]

This question already has answers here:
Why does floating-point arithmetic not give exact results when adding decimal fractions?
(31 answers)
swift: issue in converting string to double
(2 answers)
Closed 6 years ago.
I would like to round a double with 2 decimals.
I have tried many ways
- round * 100 /100
- by passing with a string
but it's the same matter
I would like 166.67 but I get 166.66999999
(look at the picture)

Swift rand is not generating random numbers [duplicate]

This question already has answers here:
Swift rand() not being random
(3 answers)
Closed 6 years ago.
I put rand() in Xcode Playground and it prints the same number 16807 even I run many times and value did not change -> "16807"!
is this a bug?
print(rand())
Mohamed Diaa,
You can make use of library functions like arc4random() or arc4random_uniform() for generating random numbers.
let random = Int(arc4random_uniform(3))
To get random value from 0 to 3
you might have to import Darwin to use it.

why I cannot get just a number [duplicate]

This question already has answers here:
Convert output from symbolic math (sym) to float
(2 answers)
Closed 8 years ago.
I did following:
clc
clear all
I0=1.2e12;
FWHM=10e-12;
c=FWHM./2.35482;
t=0:1e-12:50e-12;
syms t
int(I0.*exp(-1.*(t-5e-12).^2./(2.*c.^2)),t,0,40e-12)
but it does not give me a simple number (just a number)
The reason why matlab does not automatically give you a number is that precision could be lost.
Suppose you have a symbolic variable with value 1/3. That has infinite accuracy at this point. Yet if you evaluate it, you would lose this precision, so that is why it is not evaluated directly.
If you want to evaluate it, you could do that of course. Try doc double,doc vpa, doc eval or doc subs. I think the first one is what you need.