printf: how to set the default number of digits used by the exponent? - perl

printf: is it possible to configure the default number of digits used by the exponent.
For portability reason I would like to set the number of digits used for exponents for exponents below 100.
On my machine the default is 2 digits
printf "%.3e\n", 342.7234;
# 3.427e+02
but in How can I convert between scientific and decimal notation in Perl? the exponent has 3 digits.

Related

perl looking to format number with leading +/- and round to 2 decimal places

I'm looking to format a number to have a leading +/- plus round to two decimals. Example:
1.01333
I can use sprintf "%2f", mynumber for the decimal option
I can use sprintf "%d", mynumber for the leading +/-
But I cannot seem to find a way to combine them so I have a final number of
+1.01
Is this possible?
sprintf "%+.2f"
The '+' indicates to print the sign even if positive, and '.2f' prints 2 digits after the decimal point and left-justifies as many digits before the decimal point as there are. If you want it right-justified, then use something like
sprintf "%+9.2f"

Matlab Precision is only 8 digits?

Quoting from: https://www.mathworks.com/help/symbolic/increase-precision-of-numeric-calculations.html
By default, MATLAB® uses 16 digits of precision.
But why when I write 900000000+2 (8 zeros after 9) it returns 900000002 but writing 900000000+2 (9 zeros after 9)returns 9.0000e+09
isn't this an 8 digit precision?
you use the format command to control how many digits to be printed. help formatto see more details. Try format long g and rerun your command to see more digits.
By default, MATLAB® uses 16 digits of precision.
this refers to computation precision, not the printing precision. By default, MATLAB defines variables as double, which usually is accurate up to 16 digits. But you can print such double precision number in lower previsions (controlled by the format command)

length and precision issue in Postgres

I'm using postgres sql I need 12 digits and after decimal I need only 6 digits what length & Precision should I give in columns.what datatype shold I give to cloumn.
I tried numeric as a datatype and length I give to column is 12 and precision is 6.
If you need 12 digits before the decimal and 6 digits after, you need numeric(18,6)
Quote from the manual
The scale of a numeric is the count of decimal digits in the fractional part, to the right of the decimal point. The precision of a numeric is the total count of significant digits in the whole number, that is, the number of digits to both sides of the decimal point
(Emphasis mine)
So the first number (precision) in the type definition is the total number of digits. The second one is the number of decimal digits.
If you specify numeric(12,6) you have a total of 12 digits and 6 decimal digits, which leaves you only 6 digits for the digits to the left of the decimal. Therefor you need numeric(18,6)

how to remove last zero from number in matlab

If I set a variable in Matlab, say var1 = 2.111, after running the script, Matlab returns var1 = 2.1110. I want Matlab to return the original number, with no trailing zero. Anyone know how to do this. Thanks in advance.
By default Matlab displays results in Short fixed decimal format, with 4 digits after the decimal point.
You can change that to various other format such as:
long
Long fixed decimal format, with 15 digits after the decimal point for double values, and 7 digits after the decimal point for single values.
3.141592653589793
shortE
Short scientific notation, with 4 digits after the decimal point.
Integer-valued floating-point numbers with a maximum of 9 digits do not display in scientific notation.
3.1416e+00
longE
Long scientific notation, with 15 digits after the decimal point for double values, and 7 digits after the decimal point for single values.
Integer-valued floating-point numbers with a maximum of 9 digits do not display in scientific notation.
3.141592653589793e+00
shortG
The more compact of short fixed decimal or scientific notation, with 5 digits.
3.1416
longG
The more compact of long fixed decimal or scientific notation, with 15 digits for double values, and 7 digits for single values.
3.14159265358979
shortEng
Short engineering notation, with 4 digits after the decimal point, and an exponent that is a multiple of 3.
3.1416e+000
longEng
Long engineering notation, with 15 significant digits, and an exponent that is a multiple of 3.
3.14159265358979e+000
However I don't think other options are available. If you absolutely want to remove those zeros you would have to cast you result in a string and remove the trailing 0 characters and then display your result as a string and not a number.

num2hex vs dec2hex in MATLAB

I don't understand the difference between hex2dec and hex2num and their opposites in MATLAB.
Say I had a hex value, 3FD3B502C055FE00. When I use hex2dec, I get 4.5992e+018
. When I use hex2num, I get 0.3079. What's going on?
These functions work very differently, as you noticed. hex2dec converts a hexadecimal string to a floating-point number by raw byte conversion, and I think you found that this works as you were expecting. However, hex2num converts a hexadecimal string to its IEEE double-precision representation.
The IEEE 754 double precision standard calls for a one-bit sign, a 11-bit exponent, and a 52-bit fraction. So hex2num parses the hexadecimal in this format, yielding a very different result from hex2dec.
hex2dec -
Convert hexadecimal number string to decimal number
Description
d = hex2dec('hex_value') converts hex_value to its floating-point integer representation. The argument hex_value is a hexadecimal integer stored in a MATLAB string. The value of hex_value must be smaller than hexadecimal 10,000,000,000,000.
If hex_value is a character array, each row is interpreted as a hexadecimal string.
hex2num -
Convert hexadecimal number string to double-precision number
Description
n = hex2num(S), where S is a 16 character string representing a hexadecimal number, returns the IEEE® double-precision floating-point number n that it represents. Fewer than 16 characters are padded on the right with zeros. If S is a character array, each row is interpreted as a double-precision number.
NaNs, infinities and denorms are handled correctly.
Knowing that 3FD3B502C055FE00 is bigger than (10,000,000,000,000)16, out of range.