Shortest way to get digit number from a value - ios

Let's say I have a number like 134658 and I want the 3rd digit (hundreds place) which is "6".
What's the shortest length code to get it in Objective-C?
This is my current code:
int theNumber = 204398234;
int theDigitPlace = 3;//hundreds place
int theDigit = (int)floorf((float)((10)*((((float)theNumber)/(pow(10, theDigitPlace)))-(floorf(((float)theNumber)/(pow(10, theDigitPlace)))))));
//Returns "2"

There are probably better solutions, but this one is slightly shorter:
int theNumber = 204398234;
int theDigitPlace = 3;//hundreds place
int theDigit = (theNumber/(int)(pow(10, theDigitPlace - 1))) % 10;
In your case, it divides the number by 100 to get 2043982 and then "extracts"
the last decimal digit with the "remainder operator" %.
Remark: The solution assumes that the result of pow(10, theDigitPlace - 1) is
exact. This works because double has about 16 significant decimal digits and int on iOS
is a 32-bit number and has at most 10 decimal digits.

How about good old C?
int theNumber = 204398234;
char output[20]; //Create a string bigger than any number we might get.
sprintf(output, "%d", theNumber);
int theDigit = output[strlen(output)-4]-'0'; //index is zero-based.
That's really only 2 executable lines.
Yours is only 1 line, but that's a nasty, hard-to-understand expression you've got there, and uses very slow transcendental math.
Note: Fixed to take the 3rd digit from the right instead of the 3rd from the left. (Thanks #Maddy for catching my mistake)

Another solution that uses integer math, and a single line of code:
int theNumber = 204398234;
int result = (theNumber/100) % 10;
This is likely the fastest solution proposed yet.
It shifts the hundreds place down into the 1s place, then uses modulo arithmetic to get rid of everything but the lowest-order decimal digit.

Related

toStringAsFixed() returns a round-up value of a given number in Dart

I want to get one decimal place of a double in Dart. I use the toStringAsFixed() method to get it, but it returns a round-up value.
double d1 = 1.151;
double d2 = 1.150;
print('$d1 is ${d1.toStringAsFixed(1)}');
print('$d2 is ${d2.toStringAsFixed(1)}');
Console output:
1.151 is 1.2
1.15 is 1.1
How can I get it without a round-up value? Like 1.1 for 1.151 too. Thanks in advance.
Not rounding seems highly questionable to me1, but if you really want to truncate the string representation without rounding, then I'd take the string representation, find the decimal point, and create the appropriate substring.
There are a few potential pitfalls:
The value might be so large that its normal string representation is in exponential form. Note that double.toStringAsFixed just returns the exponential form anyway for such large numbers, so maybe do the same thing.
The value might be so small that its normal string representation is in exponential form. double.toStringAsFixed already handles this, so instead of using double.toString, use double.toStringAsFixed with the maximum number of fractional digits.
The value might not have a decimal point at all (e.g. NaN, +infinity, -infinity). Just return those values as they are.
extension on double {
// Like [toStringAsFixed] but truncates (toward zero) to the specified
// number of fractional digits instead of rounding.
String toStringAsTruncated(int fractionDigits) {
// Require same limits as [toStringAsFixed].
assert(fractionDigits >= 0);
assert(fractionDigits <= 20);
if (fractionDigits == 0) {
return truncateToDouble().toString();
}
// [toString] will represent very small numbers in exponential form.
// Instead use [toStringAsFixed] with the maximum number of fractional
// digits.
var s = toStringAsFixed(20);
// [toStringAsFixed] will still represent very large numbers in
// exponential form.
if (s.contains('e')) {
// Ignore values in exponential form.
return s;
}
// Ignore unrecognized values (e.g. NaN, +infinity, -infinity).
var i = s.indexOf('.');
if (i == -1) {
return s;
}
return s.substring(0, i + fractionDigits + 1);
}
}
void main() {
var values = [
1.151,
1.15,
1.1999,
-1.1999,
1.0,
1e21,
1e-20,
double.nan,
double.infinity,
double.negativeInfinity,
];
for (var v in values) {
print(v.toStringAsTruncated(1));
}
}
Another approach one might consider is to multiply by pow(10, fractionalDigits), use double.truncateToDouble, divide by the power-of-10 used earlier, and then use .toStringAsFixed(fractionalDigits). That could work for human-scaled values, but it could generate unexpected results for very large values due to precision loss from floating-point arithmetic. (This approach would work if you used package:decimal instead of double, though.)
1 Not rounding seems especially bad given that using doubles to represent fractional base-10 numbers is inherently imprecise. For example, since the closest IEEE-754 double-precision floating number to 0.7 is 0.6999999999999999555910790149937383830547332763671875, do you really want 0.7.toStringAsTruncated(1) to return '0.6' instead of '0.7'?

How to round a double up to a given degree of precision after the decimal point?

I need to always round the number up, with a given number of decimal places
e.g.
2000.301 -> 2000.31
2000.309 -> 2000.31
2000.999 -> 2001.00
2000.001 -> 2000.01
toStringAsFixed() // cant help with that
// need something like ceil(), but for decimal
If you add 0.5 to a number before rounding, it will always be rounded upwards.
Applying this to two decimal places, you get:
(2000.301 + 0.005).toStringAsFixed(2)
=> 2000.31
The trick is to add as many 0s between the decimal and the 5 as you want to have significant decimals in the output.
Cooking it into a utility function might look like:
import 'dart:math';
String roundToDecimals(double value, int decimals) {
final corr = 0.5 * pow(0.1, decimals);
return (value + corr).toStringAsFixed(decimals);
}

Find digit at index of NSDecimalNumber

Is it possible to find the number at an index of an NSDecimalNumber?
For example is something like this possible?
var a: NSDecimalNumber = 10.00123456789
var indexOfa = a(index: 6)
//indexOfa = 6 (6th position from the left)
I'm basically trying to make a custom rounding function that rounds down on x.xx5 and rounds up on x.xx6.
For example:
67.5558 is rounded to 67.55.
67.5568 is rounded to 67.56.
...Not a duplicate of rounding question. I'm asking specifically here how to find a digit at a specified index of an NSDecimalNumber.
Also the answer you linked doesn't answer my question. I can use the rounding behaviours for NSDecimalNumber but it rounds up on .5 I need it to round down on .5 and up on .6. I can create my own custom rounding function if I could find the index of the 2nd decimal number.
I have a solution that works. But its horrible. There's a few NSDecimalNumber extensions in there but you can probably guess what they are doing. And I'm sure theres a better way...
public func currencyRoundDown() -> NSDecimalNumber {
let rounded: NSDecimalNumber
let toThree = self.roundDown(3)
let lower = self.roundDown(2).adding(0.005)
if lower.moreThan(toThree) || lower.same(toThree) {
rounded = toThree.roundDown(2)
} else {
rounded = toThree.roundUp(2)
}
return rounded
}
Is it possible to find the number at an index of an NSDecimalNumber?
Let's consider how to do it with a Double. In this rendering, the "index" counts from the decimal point.
Multiply by 10 to the index, to bring the index digit into the 1s place. Now take the remainder modulo 10. That is the digit in question.
let d = 1234.56789 // the 5 is 1, the 6 is 2, the 7 is 3 ...
let place = 3
let shift = pow(10.0, Double(place)) * d
let digit = Int(shift) % 10 // 7

1 000 000 000 000 000 000 th fibonacci number

The problem is Get Under 1,000,000,000,000,000,000th fibonacci number%1,000,000
#include <iostream>
#define fibo(a,b) {long long c=b;b=a;a=(b+c)%1000000;}
using namespace std;
int main(){
long long a=1,b=0; //two num
long long pa,pb,n,k,arr[2][1000]; //last two num,input,input<=2^k
cin>>n;
arr[0][0]=n/2;arr[1][0]=n%2;
for(unsigned long long i=1;n>3;i++){
arr[0][i]=arr[0][i-1]/2;
arr[1][i]=arr[0][i-1]%2;
if(arr[0][i]==1){
k=i;
break;
}
}
if(n<=3){ //special occasions
switch(n){
case 0:cout<<"0"<<endl;break;
case 3:cout<<"2"<<endl;break;
default:cout<<"1"<<endl;
}
return 0;
}
while(k>=0){ //calc
pa=a;pb=b;
a=((pa+pb*2)*pa)%1000000; //F(2n)=(F(n)+F(n-1)*2)*F(n)
b=(pa*pa+pb*pb)%1000000; //F(2n-1)=F(n)^2+F(n-1)^2
if(arr[1][k--]==1){fibo(a,b);} //F(n+1)=F(n)+F(n-1)
}
cout<<a<<endl;
return 0;
}
when is it wrong?
And why is it wrong?
I can't find different occasion.
An alternative approach you could consider using here is the fact that there are only 1,000,000 possible remainders for Fibonacci numbers, so if you were to compute the first 1,000,001 Fibonacci numbers, at some point you would find that the numbers would start to go in a cycle. So consider the following approach:
Compute the first 1,000,001 Fibonacci numbers.
The numbers will eventually enter a cycle. Determine how many steps k are needed to enter the cycle and how long l the cycle is.
The 1,000,000,000,000,000,000th Fibonacci number, mod 1,000,000, can be found by determining what position the 1,000,000,000,000,000,000th Fibonacci number would end up in the cycle, which is the ((1,000,000,000,000,000,000 - k) % l)th position. So look at that position and output the entry there.

objective c float increment wrong value

I have a query regarding floating value increment in loop.
I have following code
float add = 1.02f;
float counter = 0.0f;
for (int i = 0; i < 20; i++) {
counter += add;
NSLog(#"%f",counter);
}
While executing this loop I am getting following result
1.020000
2.040000
3.060000
4.080000
5.100000
6.120000
7.140000
8.160000
9.180000
10.200001
11.220001
12.240002
13.260002
14.280003
15.300003
16.320004
17.340004
18.360004
19.380005
20.400005
Here is expected result
1.020000
2.040000
3.060000
4.080000
5.100000
6.120000
7.140000
8.160000
9.180000
10.200000
11.220000
12.240000
13.260000
14.280000
15.300000
16.320000
17.340000
18.360000
19.380000
20.400000
Why i am getting some floating point in loop without adding it.
I need to loop more then 1000 times. And I want the value in float variable.
Thanks in advance.
This happens because float cannot represent the values that you have with exact precision. There are two simple ways of fixing this:
Represent the number as 100 times the target value, and use integers - 1.02 becomes 102, 2.04 becomes 204, and so on.
Use NSDecimalNumber to represent your numbers - Unlike float, NSDecimalNumber can represent all your values with full precision.
Here is how to implement the first approach:
int add = 102;
int counter = 0;
for (int i = 0; i < 20; i++) {
counter += add;
NSLog(#"%d.%d", counter/100, counter%100);
}
Here is how to implement the second approach:
NSDecimalNumber add = [NSDecimalNumber decimalNumberWithString:#"1.02"];
NSDecimalNumber counter = [NSDecimalNumber zero];
for (int i = 0; i < 20; i++) {
counter = [counter decimalNumberByAdding:add];
NSLog(#"%#", counter);
}
Why i am getting some floating point in loop without adding it.
Because float is a binary type that doesn't represent decimal values exactly. Rather than trying to explain completely and correctly, let me point you to the well-known paper What Every Computer Scientist Should Know About Floating Point Arithmetic.
Floating point number representations in computers are approximations, they are not exact. Sometimes you end up trying to display a number that can't be exactly represented in the computer's floating point number implementation, so it gives you an approximation. Also you get small arithmetic errors from repeated multiplications, additions, etc. of floating point numbers. The best you can do is to use doubles, which have more precision than floats do. In special circumstances, you could also represent your data in a different format and just change how you display it to the user to fit what they expect. For example, when working with dollars and cents, you could just store a total as a number of cents (which would be only an integer) and then format it to be shown as dollars and cents correctly for the user. There's no floating point rounding issues happening then.
Floating point numbers use four bytes = 32 bits.
1 bit for sign
8 bits for exponent
23 bits for mantissa
Precision: The number of decimal digits precision is calculated via number_of_mantissa_bits * Log10(2). Thus ~7.2 and ~15.9 for single and double precision respectively.
That's why you start to see rounding errors on the 7th digit
Source link.

Resources