Scientific notation with three significant figures - objective-c

Is there a way to use scientific notation in objective c and have it display three significant digits only? What I am current using is:
string = [NSString stringWithFormat:#"%e", floatNumber];
// floatNumber = 100000; string = 1.000000e+06
I just want string = 1.00e+06

Use the format specifier ".2" as follows:
string = [NSString stringWithFormat:#"%.2e", floatNumber];
From apple's documentation:
The format specifiers supported by the NSString formatting methods and CFString formatting functions follow the IEEE printf specification...
And from the IEEE printf specification, if you read under the Description section, you will find:
e, E
The double argument shall be converted in the style "[-]d.ddde±dd", where there is one digit before the radix character (which is non-zero if the argument is non-zero) and the number of digits after it is equal to the precision; if the precision is missing, it shall be taken as 6; if the precision is zero and no '#' flag is present, no radix character shall appear. The low-order digit shall be rounded in an implementation-defined manner. The E conversion specifier shall produce a number with 'E' instead of 'e' introducing the exponent. The exponent shall always contain at least two digits. If the value is zero, the exponent shall be zero.

Related

Objective-C float being limited to 6 decimal places? [duplicate]

This question already has answers here:
What's the difference between float and double?
(3 answers)
Closed 8 years ago.
I have an Objective-C project that needs to display numbers like 0.00000217, and very small numbers like that. Problem is, Objective-C is rounding this to the 6th decimal place so it displays as 0.000002. Is there a type to display more decimal places? My code:
float floatValueOfSmallNumber = [value floatValue];
[theLabel setText:[NSString stringWithFormat:#"%f", floatValueOfSmallNumber]];
Thanks!
While a float only has ~7 significant decimal digits, that's not the problem you are running up against here; 0.00000217 has only three significant digits, after all.
You are using the %f format specifier which is inherited from C and defined thus (7.21.6 Formatted input/output functions):
A double argument representing a floating-point number is converted to decimal notation in the style [−]ddd.ddd, where the number of digits after the decimal-point character is equal to the precision specification. If the precision is missing, it is taken as 6; if the precision is zero and the # flag is not specified, no decimal-point character appears. If a decimal-point character appears, at least one digit appears before it. The value is rounded to the appropriate number of digits.
Using double won't change this; instead, you need to change your format specifier. You can use %e or %g if you don't mind scientific notation, but another alternative would be to use a precision specifier: %.10f, for example, will print ten decimal digits.

What does \0 stand for? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What does the \0 symbol mean in a C string?
I am new at iPhone Development. I want to know, what does '\0' means in C, and what is the equivalent for that in objective c.
The null character '\0' (also null terminator), abbreviated NUL, is a control character with the value zero. Its the same in C and objective C
The character has much more significance in C and it serves as a reserved character used to signify the end of a string,often called a null-terminated string
The length of a C string (an array containing the characters and terminated with a '\0' character) is found by searching for the (first) NUL byte.
In C, \0 denotes a character with value zero. The following are identical:
char a = 0;
char b = '\0';
The utility of this escape sequence is greater inside string literals, which are arrays of characters:
char arr[] = "abc\0def\0ghi\0";
(Note that this array has two zero characters at the end, since string literals include a hidden, implicit terminal zero.)
The '\0' inside character literals and string literals stands for the character with the code zero. The meaning in C and in Objective C is identical.
To illustrate, you can use \0 in an array initializer to construct an array equivalent to a null-terminated string:
char str1[] = "Hello";
char str2[] = {'H', 'e', 'l', 'l', 'o', '\0'};
In general, you can use \ooo to represent an ASCII character in octal notation, where os stand for up to three octal digits.
To the C language, '\0' means exactly the same thing as the integer constant 0 (same value zero, same type int).
To someone reading the code, writing '\0' suggests that you're planning to use this particular zero as a character.
\0 is zero character. In C it is mostly used to indicate the termination of a character string. Of course it is a regular character and may be used as such but this is rarely the case.
The simpler versions of the built-in string manipulation functions in C require that your string is null-terminated(or ends with \0).
In C \0 is a character literal constant store into an int data type that represent the character with value of 0.
Since Objective-C is a strict superset of C this constant is retained.
It means '\0' is a NULL character in C, don't know about Objective-C but its probably the same.

Convert double decimal to Hex, Binary and Octal strings

Is there a custom or standard library that I can use with objective c to convert a decimal floating point value into Hexadecimal, Binary and Octal NSStrings?
I've been looking around a lot but can only find how to convert the other direction (from everything else into decimal), by using: double result = (double)strtoll(myHexString.UTF8String, NULL,16)
As for octal and hex, you can exploit the fact that you can format numbers in hex and octal using the %x and %o specifiers:
NSString* str=[NSString stringWithFormat: #"Hex: %x Octal: %o",14,14];
But this only for the integral part, so you can use integers.If you want also the decimal part, to obtain fixed points you need to implement the algorithm, same for binary.
In order to complement the previous answer, the format specifier %x allows to treat integer value only.
If you get a decimal value greater than the max integer value you can use long long to cast your double value by this way: NSString * hexString = [NSString stringWithFormat:#"%llx", (long long)doubleValue];

What does stringWithFormat:#"%#-1" mean?

I'm reading someone elses code and they are using %#-1 to format an integer. I can't find anything on Google since it ignores symbols. Anyone else had more experience at string formatting than me?
[NSString stringWithFormat:#"%#-1", subnumber]
Thanks!
According to the specification:
Each conversion specification is introduced by the '%' character, or by the character sequence "%n$", after which the following appear in sequence:
Zero or more flags (in any order), which modify the meaning of the conversion specification.
An optional minimum field width. If the converted value has fewer bytes than the field width, it shall be padded with spaces by default on the left; it shall be padded on the right if the left-adjustment flag ( '-' ), described below, is given to the field width. The field width takes the form of an asterisk ( '*' ), described below, or a decimal integer.
An optional precision that gives the minimum number of digits to appear for the d, i, o, u, x, and X conversion specifiers; the number of digits to appear after the radix character for the a, A, e, E, f, and F conversion specifiers; the maximum number of significant digits for the g and G conversion specifiers; or the maximum number of bytes to be printed from a string in the s [XSI] [Option Start] and S [Option End] conversion specifiers. The precision takes the form of a period ( '.' ) followed either by an asterisk ( '*' ), described below, or an optional decimal digit string, where a null digit string is treated as zero. If a precision appears with any other conversion specifier, the behavior is undefined.
An optional length modifier that specifies the size of the argument.
A conversion specifier character that indicates the type of conversion to be applied.
We're using a conversion of the first type, since there's no dollar sign in here. Note the words in sequence at the top of the above list. The # is a conversion specifier character (as mentioned here), which indicates that we should access the value passed in as an NSObject and read its description property. Since we've already reached the last bullet point, the format code actually ends after the # symbol, and as #Kevin Ballard pointed out, the -1 is parsed as literal text.
That's just going to print "NUM-1" (where NUM is the number). To give an example, if the number is 5, that will print "5-1".
When using format strings, any modifiers to the format token must occur before the format type specifier. In this case, that means any modifiers to the %# token must occur between the % and the # (though I'm not sure if there are actually any modifiers that %# accepts).
subnumber is probably object of class like NSNumber. Like we use %d for int, %f for float, %# is place holder for a refrence. In that case
NSNumber *subnumber = [NSNumber numberWithInt:5];
NSLog([NSString stringWithFormat:#"%#-1", subnumber]);
will print '5-1'

Showing decimals of a variable with sprintf in MATLAB

I don't understand the next thing that happens using the sprintf command.
>> vpa(exp(1),53)
ans =
2.7182818284590455348848081484902650117874145507812500
>> e = 2.7182818284590455348848081484902650117874145507812500
e =
2.7183
>> sprintf('%0.53f', e)
ans =
2.71828182845904550000000000000000000000000000000000000
Why does sprintf show me the number e rounded instead of the number and I kept at the first place?
Variables are double precision by default in MATLAB, so the variable e that you create is limited to the precision of a double, which is about 16 digits. Even though you entered more digits, a double doesn't have the precision to accurately represent all those extra digits and rounds off to the nearest number it can represent.
EDIT: As explained in more detail by Andrew Janke in his answer to this follow-up question I posted, the number you chose for e just happens to be an exact decimal expansion of the binary value. In other words, it's the exactly-representable value that a nearby floating-point number would get rounded to. However, in this case anything more than approximately 16 digits past the decimal point is not considered significant since it can't really be represented accurately by a double-precision type. Therefore, functions like SPRINTF will automatically ignore these small values, printing zeroes instead.