I am trying to add fractions to get a value to put in for my progress bar but am just getting 0. here is what I have and what it is logging.
NSLog(#"prog::%f",self.progView.progress);
int total = self.forms.count;
float calc = (float)(self.progView.progress / total) + (1/total);
NSLog(#"calc::%f",calc);
NSLog(#"total::%d",total);
self.progView.progress = calc;
NSLog(#"prog now:%f", self.progView.progress);
Log:
prog::0.000000
calc::0.000000
total::4
prog now:0.000000
Take care of where you place your cast to float. Try changing from this:
float calc = (float)(self.progView.progress / total) + (1/total);
To this:
float calc = ((float)self.progView.progress / (float)total) + (1/(float)total);
Alternatively, make total a float rather than an int, e.g.
float total = self.forms.count;
float calc = ((float)self.progView.progress / total) + (1 / total);
Related
I have 2 sliders, both sliders have a minimum and maximum value set to 0 and 1. In the pic, they simulate changing ramp values at a maximum of 300 total frames combined.
For the first slider, I get the range of 0-150 frames correctly with
int selectedFrameCount = 300;
int currentSelectedFrameValue = sender.value * (selectedFrameCount/2);
I'd like to know how to get a range of 150-300 frames with the second slider without changing the minimum and maximum UISlider values because I need those values for a separate calculation.
For a separate function, I'll need to convert that value back to from selected 150-300 value into a float based on the 0-1 value selected.
example: 285 = x * (300/2) + 150;
Isn't it as simple as
int currentSelectedFrameValue = (sender == firstSlider ? 0 : selectedFrameCount/2)
+ sender.value * (selectedFrameCount/2);
?
You have to imagine your calc as a function (it is, so you haven't to imagine it :D )
y is the int you want
x is the sender.vale
y(x) = x * (selectedFrame/2)
If you want to translate your line on y-axis by selectedFrame/2 you just have to add it to as q value on linear equation y(x) = mx + q
So, the solution is
int value = sender.value * (selectedFrame/2) + (selectedFrame/2)
To convert the int value into a float value in 0-1 range, use this
multipliedMaxValue = selectedFrame
multipliedSelectedValue = value
multipliedMaxValue : 1 = multipliedSelectedValue : x
if you want x, just do
float toRangeValue = (float)multipliedSelectedValue / (float)multipliedMaxValue;
Can any one help me , How do I implement the area calculator using in group of latitude and longitudes .
For Example One person walk around the building , I am getting the latitude and longitudes while walking time . I want calculate the how much square feet that building was constructed.
Well, I´m going to try to help you, but I´m not going to give the complete answer.
I think, the first step is convert lats and longs in a cartesian coordinate system. You should calculate the center of all points. (A simple median).
Second step, convert all points to ENU coordinates centered in its center:
This step, I did, and here you are:
Constants:
#define DEGREES_TO_RADIANS (M_PI/180.0)
#define WGS84_A (6378137.0) // WGS 84 semi-major axis constant in meters
#define WGS84_E (8.1819190842622e-2) // WGS 84 eccentricity
Structs:
//To change to ECEF
typedef struct{
double x;
double y;
double z;
} ECEFCoordinate;
typedef struct{
double east;
double north;
double up;
} ENUCoordinate;
Methods, (you need past through ECEF):
#pragma mark Geodetic utilities definition
-(ECEFCoordinate) ecefFromLatitude:(double)lat longitude:(double)lon andAltitude:(double)alt
{
double clat = cos(lat * DEGREES_TO_RADIANS);
double slat = sin(lat * DEGREES_TO_RADIANS);
double clon = cos(lon * DEGREES_TO_RADIANS);
double slon = sin(lon * DEGREES_TO_RADIANS);
double N = WGS84_A / sqrt(1.0 - WGS84_E * WGS84_E * slat * slat);
ECEFCoordinate ecef;
ecef.x = (N + alt) * clat * clon;
ecef.y = (N + alt) * clat * slon;
ecef.z = (N * (1.0 - WGS84_E * WGS84_E) + alt) * slat;
return ecef;
}
// Converts ECEF to ENU coordinates centered at given lat , lon (with ECEFCenter)
-(ENUCoordinate)enuFromECEFCenter:(ECEFCoordinate)ecefCenter withLat:(double)lat andLon:(double)lon fromEcef:(ECEFCoordinate)ecef
{
double clat = cos(lat * DEGREES_TO_RADIANS);
double slat = sin(lat * DEGREES_TO_RADIANS);
double clon = cos(lon * DEGREES_TO_RADIANS);
double slon = sin(lon * DEGREES_TO_RADIANS);
double dx = ecefCenter.x - ecef.x;
double dy = ecefCenter.y - ecef.y;
double dz = ecefCenter.z - ecef.z;
ENUCoordinate enu;
enu.east = -slon*dx + clon*dy;
enu.north = -slat*clon*dx - slat*slon*dy + clat*dz;
enu.up = clat*clon*dx + clat*slon*dy + slat*dz;
return enu;
}
Last step: (I think the easy way is use triangles, from center to two consecutive points), calculate the area of a bunch of points in a cartesian coordinate system (east,north). Same than (x,y).
Good luck.
Last help two calculate the Area, I think you can find more help (and maybe best way) trough internet.
In xcode I am utilizing 4 utitextfields for the following chart that the user will enter a whole number, chart is the following:
Total | Win | Place | Show
34 | 4 | 5 | 3
My algorithm for this chart is the following:
-For total win * 1
-For total place * .5
-For total Show * .25
-Add the three totals into a new Float value (identified as,
float totalTrackAllTogether)
-Divide totalTrackAllTogether by the total column (identified as,
int trackTotalColum)
-Display the sum on onOffLabel.text
Here is the code portion that does not seem to work and crashes
-(void)trackRecordMath
{
int trackWinColum = [trackWin.text intValue];
int trackPlaceColum = [trackPlace.text floatValue];
int trackShowColum = [trackShow.text floatValue];
int trackTotalColum= [trackTotal.text intValue];
int trackWinTotal = trackWinColum * 1;
NSLog(#"%d", trackWinTotal);
int trackPlaceTotal = trackPlaceColum *.5;
NSLog(#"%d", trackPlaceTotal);
int trackShowTotal= trackShowColum *.25;
NSLog(#"%d", trackShowTotal);
float totalOfTrackColums = trackWinTotal + trackPlaceTotal + trackShowTotal;
float totalTrackAllTogether= (float) totalOfTrackColums / trackTotalColum;
onOffLabel.text = [[NSString alloc] initWithFormat:#"%f", totalTrackAllTogether];
}
I seem to be held up when trying to multiply floating points with whole numbers, and displaying it through onOffLabel.text. Thank you for the help!
These is basic stuff which works the same in C, C++ and Objective-C. If you do a floating-point calculation and store the result in an int, everything after the decimal point is dropped.
Furthermore, by using float instead of double you get massive rounding errors without any benefit. Instead of
int trackPlaceColum = [trackPlace.text floatValue];
write own of these:
NSInteger trackPlaceColumn = [trackPlace.text integerValue];
double trackPlaceColumn = [trackPlace.text doubleValue];
BTW. It's a column, not a colum. And you will get a crash if you divide by zero. So check whether trackTotalColumn is zero or not in your code.
Doing this:
float x = arc4random() % 100;
returns a decent result of a number between 0 and 100.
But doing this:
float x = (arc4random() % 100)/100;
Returns 0. How can I get it to return a float value?
Simply, you are doing integer division, instead of floating point division, so you are just getting a truncated result (.123 is truncated to 0, for example). Try
float x = (arc4random() % 100)/100.0f;
You are dividing an int by an int, which gives an int. You need to cast either to a float:
float x = (arc4random() % 100)/(float)100;
Also see my comment about the modulo operator.
To get a float division instead of an integer division:
float x = arc4random() % 100 / 100.f;
But be careful, using % 100 will only give you a value between 0 and 99, so dividing it by 100.f will only produce a random value between 0.00f and 0.99f.
Better, to get a random float between 0 and 1:
float x = arc4random() % 101 / 100.f;
Even better, to avoid modulus bias:
float x = arc4random_uniform(101) / 100.f;
Alternatively, to avoid a two digits precision bias:
float x = (float)arc4random() / UINT32_MAX;
And in Swift 4.2+, you get built-in support for ranges:
let x = Float.random(in: 0.0...1.0)
In Swift:
Float(arc4random() % 100) / 100
I'm making a simple pong game. To make the ball move at the beginning of a new round, I am using
ballVelocity = CGPointMake(4 - arc4random() % 8,4 - arc4random() % 8);
However, the important part is just this:
4 - arc4random() % 8
However, there are a few problems with this: first and foremost, it doesn't really generate a random number. Only after I quit the simulator, then reopen it are new numbers generated. Secondly, I only want it to generate numbers between -4 and -2 or 2 and 4.
arc4random() is the preferred random function on the iphone, instead of rand(). arc4random() does not need seeding.
This code will generate the ranges you're interested in:
int minus2_to_minus4 = (arc4random() % 3) - 4;
int two_to_four = (arc4random() % 3) + 2;
You need to look at the rand() function. Basically, you "seed" it with a start value, and it returns a new random number every time you call it.
Or look at this question which has a full example using arc4random.
This will give you a floating point number between -4 and -2 OR 2 and 4
float low_bound = -4; //OR 2
float high_bound = -2;//OR 4
float rndValue = (((float)arc4random()/0x100000000)*(high_bound-low_bound)+low_bound);
If you want a number in -4…-2 AND 2…4 try this:
float low_bound = 2;
float high_bound = 4;
float rndValueTemp = (((float)arc4random()/0x100000000)*(high_bound-low_bound)+low_bound);
float rndValue = ((float)arc4random()/0x100000000)<0.5?-rndValueTemp:rndValueTemp;