how to convert a float value to rounded off in iphone app - iphone

I have a float value i want to rounded off to nearest total i have find some math function but not working
Now potential profit is 300.52 so how to roundded of this please
float potentialprofit=otherthanCereniaAnnual*totalProfit;
NSString*potentialProfitTitle=[NSString stringWithFormat:#"%.2f",potentialprofit];
[potentialProfit setTitle:potentialProfitTitle forState:UIControlStateNormal];

int result = (int)ceilf(myFloat );
int result = (int)roundf(myFloat );
int result = (int)floor(myFloat);
float result = ceilf(myFloat );
float result = roundf(myFloat );
float result = floor(myFloat);
I think it will be helpful to you.

try this:
float f = ...;
f = (int)(f+0.5);
if you want to handle negative value, try this:
f = (int)(f < 0 ? f-0.5 : f+0.5);

Related

How does the reversebits function of HLSL SM5 work?

I am trying to implement an inverse FFT in a HLSL compute shader and don't understand how the new inversebits function works. The shader is run under Unity3D, but that shouldn't make a difference.
The problem is, that the resulting texture remains black with the exception of the leftmost one or two pixels in every row. It seems to me, as if the reversebits function wouldn't return the correct indexes.
My very simple code is as following:
#pragma kernel BitReverseHorizontal
Texture2D<float4> inTex;
RWTexture2D<float4> outTex;
uint2 getTextureThreadPosition(uint3 groupID, uint3 threadID) {
uint2 pos;
pos.x = (groupID.x * 16) + threadID.x;
pos.y = (groupID.y * 16) + threadID.y;
return pos;
}
[numthreads(16,16,1)]
void BitReverseHorizontal (uint3 threadID : SV_GroupThreadID, uint3 groupID : SV_GroupID)
{
uint2 pos = getTextureThreadPosition(groupID, threadID);
uint xPos = reversebits(pos.x);
uint2 revPos = uint2(xPos, pos.y);
float4 values;
values.x = inTex[pos].x;
values.y = inTex[pos].y;
values.z = inTex[revPos].z;
values.w = 0.0f;
outTex[revPos] = values;
}
I played around with this for quite a while and found out, that if I replace the reversebits line with this one here:
uint xPos = reversebits(pos.x << 23);
it works. Although I have no idea why. Could be just coincidence. Could someone please explain to me, how I have to use the reversebits function correctly?
Are you sure you want to reverse the bits?
x = 0: reversed: x = 0
x = 1: reversed: x = 2,147,483,648
x = 2: reversed: x = 1,073,741,824
etc....
If you fetch texels from a texture using coordinates exceeding the width of the texture then you're going to get black. Unless the texture is > 1 billion texels wide (it isn't) then you're fetching well outside the border.
I am doing the same and came to the same problem and these answers actually answered it for me but i'll give you the explanation and a whole solution.
So the solution with variable length buffers in HLSL is:
uint reversedIndx;
uint bits = 32 - log2(xLen); // sizeof(uint) - log2(numberOfIndices);
for (uint j = 0; j < xLen; j ++)
reversedIndx = reversebits(j << bits);
And what you found/noticed essentially pushes out all the leading 0 of your index so you are just reversing the least significant or rightmost bits up until the max bits we want.
for example:
int length = 8;
int bits = 32 - 3; // because 1 << 3 is 0b1000 and we want the inverse like a mask
int j = 6;
and since the size of an int is generally 32bits in binary j would be
j = 0b00000000000000000000000000000110;
and reversed it would be (AKA reversebits(j);)
j = 0b01100000000000000000000000000000;
Which was our error, so j bit shifted by bits would be
j = 0b11000000000000000000000000000000;
and then reversed and what we want would be
j = 0b00000000000000000000000000000011;

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);

Objective-C Float Rounding

How might I round a float to the nearest integer in Objective-C:
Example:
float f = 45.698f;
int rounded = _______;
NSLog(#"the rounded float is %i",rounded);
should print "the rounded float is 46"
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.
The recommended way is in this answer: https://stackoverflow.com/a/4702539/308315
Original answer:
cast it to an int after adding 0.5.
So
NSLog (#"the rounded float is %i", (int) (f + 0.5));
Edit: the way you asked for:
int rounded = (f + 0.5);
NSLog (#"the rounded float is %i", rounded);
For round float to nearest integer use roundf()
roundf(3.2) // 3
roundf(3.6) // 4
You can also use ceil() function for always get upper value from float.
ceil(3.2) // 4
ceil(3.6) // 4
And for lowest value floor()
floorf(3.2) //3
floorf(3.6) //3
The easiest way to round a float in objective-c is lroundf:
float yourFloat = 3.14;
int roundedFloat = lroundf(yourFloat);
NSLog(#"%d",roundedFloat);
Check the manual page for rint()
If in case you want round float value in integer below is the simple method for rounding the float value in objective C.
int roundedValue = roundf(Your float value);
let's do tried and checkout
//Your Number to Round (can be predefined or whatever you need it to be)
float numberToRound = 1.12345;
float min = ([ [[NSString alloc]initWithFormat:#"%.0f",numberToRound] floatValue]);
float max = min + 1;
float maxdif = max - numberToRound;
if (maxdif > .5) {
numberToRound = min;
}else{
numberToRound = max;
}
//numberToRound will now equal it's closest whole number (in this case, it's 1)

How can I draw a graph with a date axis using C++ and ZedGraph?

I have successfully used the C++ example project to draw graphs from my C++ project using ZedGraph. However there is no example with Date axis for C++.
The following code is taken from the C# example found at
http://zedgraph.org/wiki/index.php?title=Tutorial:Date_Axis_Chart_Demo. Please see my comments with the text //JEM// to see where my problem is
PointPairList list = new PointPairList();
for ( int i=0; i<36; i++ )
{
double x = (double) new XDate( 1995, 5, i+11 );
> //JEM //This line above doesn't work in
> C++.
double y = Math.Sin( (double) i * Math.PI / 15.0 );
list.Add( x, y );
}
....missing code...
// Set the XAxis to date type
myPane.XAxis.Type = AxisType.Date;
//JEM //This one also doesn't work even if I change it to the
//syntax that C++ understands, that is,
myPane->XAxis->Type = AxisType->Date;
Maybe C++ has some problems with the anonymous variables?
Try to create a XDate object first before converting it to double.
XDate date = new XDate( 1995, 5, i+11 );
double x = (double)date;
Thanks Gacek.
This is how it finally went down. Your answer was the turning point!!!
for ( int i = 0; i < basin.DY; i++ ){
XDate dato(1995,9,i,0,0,0); //date
double x = (double)dato;
//double x = i;
double y = basin.Qsim[i];
double y2 = basin.Qobs[i];
list->Add( x, y );
list2->Add( x, y2 );
}
//set the XAXis to date type
myPane->XAxis->Type = AxisType::Date;
here is the constructor for the Xdate type for c++ from sourceforge dot net documentation/html/M_ZedGraph_XDate__ctor_3.htm.
XDate (int year, int month, int day, int hour, int minute, double second)
I have also found a detailed example on this link http://www.c-plusplus.de/forum/viewtopic-var-t-is-186422-and-view-is-next.html
with the following code
/// ZedGraph Kurve //////////
private: void CreateGraph( ZedGraphControl ^zgc )
{
GraphPane ^myPane = zgc->GraphPane;
// Set the titles and axis labels
myPane->Title->Text = "Gewichtskurve";
myPane->XAxis->Title->Text = "Tag";
myPane->YAxis->Title->Text = "Gewicht in Kg";
// Make up some data points from the Sine function
double x,y;
PointPairList ^list = gcnew PointPairList();
for ( int i=0; i<36; i++ )
{
x = (double) gcnew XDate( 1995, 5, i+11 );
y = Math::Sin( (double) i * Math::PI / 15.0 );
list->Add( x, y );
}
// Generate a blue curve with circle symbols, and "My Curve 2" in the legend
LineItem ^myCurve = myPane->AddCurve( "Trainingskurve", list, Color::Blue,
SymbolType::Circle );
XDate ^xdatum = gcnew XDate ( 1995, 1, 1);
xdatum->AddDays ( 1 );
myPane->XAxis->Type = AxisType::Date;