Regex for float(x,y) possible? - html

In the following, float(x,y) means a number with x total digits and y decimals.
I am trying to do client side validation for an HTML Input field. The field corresponds to a MySQL column with data type float(x,y). I know I can define a pattern like float(5,2) with lots of 'ORs'. Is there an efficient way of generating a Regex for this such that I can encode it in my web document?
Something of a workaround is to specify \d+(\.\d{1,y})? and then set maxlength=x+1. Should be x+1 because the decimal place is counted. This would then allow the submit of an integer of length x+1 contrary to specification. I realize I can do JavaScript validation but I would like to achieve the desired with HTML validation.

You can first check the total length with a lookahead and then check for the length of numbers after the decimal point.
If you want X total number of digits and at most Y decimal points, you can use:
^(?=.{X+1}$)([1-9]\d*|0)\.\d{1,Y}$
Explanation:
^ asserts you are in the start position of the line
(?= lookahead (zero length) match for:
.{X + 1} X+1 characters
$ end of line //now you have asserted the total length
the whole part either
0 is a single zero
[1-9]\d* more than a single digit and does not start with zero
\. a dot for the decimal point
\d{1,Y} at least 1 and at most Y digits
$ asserts end of line
Note that you do not need to check the length of the whole part since you are already checking for the total length and the length of digits after the decimal so the part before the decimal point is automatically correct.
Example:
For X = 5 and Y = 2, you will have:
^(?=.{8}$)([1-9]\d*|0)\.\d{1,2}$
Regex101 demo

Related

Regex of max 5 decimal separate by +

i'm trying to make a regex to add to a input pattern (HTML) to check if is valid,
it need to be valid only if the input contain a string composed by decimal(with 1 or 2 number after comma) or integer number separated by a +
and maximum of 5 number
and it can not start or end with a + or it can not be possible to have a number with comma without number after (i use comma instead of dot for decimal)
for example
10+5,1+6,20 OK
10 OK
6+4+8,9+3+9+3 NO
10,2+4+6+ NO
10,+5 NO
i've tried with something like this but id doesn't work very well
((\d{1,3}|(\d*,\d{1,2})*)+(\+)?){1,5}
also i've tried with this:
^((\s*)|([0-9]\d{0,9}(\,\d{1,2})?%?))*(\+((\s*)|([0-9]\d{0,9}(\,\d{1,2})?%?))+){0,4}$
but it doesn't work very well with the 2 digit max for the decimal and ending +
any suggestions ??
i've made some test here:
https://regexr.com/5jsfv
it should pass the first 3 and faile on the last 4
thanks
You can use
^\d+(?:,\d{1,2})?(?:\+\d+(?:,\d{1,2})?){0,4}$
In the HTML pattern attribute use it as
pattern="\d+(?:,\d{1,2})?(?:\+\d+(?:,\d{1,2})?){0,4}"
See the regex demo.
NOTE: If you want to limit the number of digits in the integer part to be max 3, replace the \d+ with \d{1,3}:
^\d{1,3}(?:,\d{1,2})?(?:\+\d{1,3}(?:,\d{1,2})?){0,4}$
Details:
^ - start of string (implicit in pattern regex)
\d+(?:,\d{1,2})? - one or more digits and then an optional sequence of a , and one or two digits
(?:\+\d+(?:,\d{1,2})?){0,4} - zero to four occurrences of a + char followed with one or more digits and then an optional sequence of a , and one or two digits
$ - end of string (implicit in pattern regex)

ActionScript: Number.toExponential returns incorrect results for some values

I found at least two numbers for which ActionScript's Number.toExponential(20) returns incorrect results.
The most obvious value is 0.
trace(Number(0).toExponential(20)); // 0.00000000000000000000e-16
trace(Number(0).toExponential(2)); // 0.00e-16
trace(Number(0).toExponential(1)); // 0.0e-16
trace(Number(0).toExponential(0)); // 1e-15 - even worse!
I've made a class Double for playing around with Numbers (which are IEEE 754 double-precision binary floating point format numbers). It takes a Number and extracts all the bits into sign, bits and significand (the latter is printed including the implicit leading bit), or can generate a Number provided sign, bits and significand. There are methods to get the next and prior nearest representable numbers.
Here's what 0 looks like internally:
[Double number=0.00000000000000000000e-16 sign=1 exponent=-1023 isZero
significand=00000000000000000000000000000000000000000000000000000]
The other incorrect results are the nearest representable Number greater than Number.MAX_VALUE / 2 and Number.MAX_VALUE / 4.
// Number.MAX_VALUE / 2.
trace(Number(8.98846567431157854072e+307).toExponential(20)); // 8.98846567431157854072e+307
// Nearest representable Number greater than Number.MAX_VALUE / 2. Printed incorrectly!
trace(Number(8.98846567431157953864e+307).toExponential(20)); // 0.e+327
// A Number two ULPs greater than Number.MAX_VALUE / 2.
trace(Number(8.98846567431158153448e+307).toExponential(20)); // 8.98846567431158153448e+307
// Nearest representable Number greater than Number.MAX_VALUE / 4. Printed incorrectly!
trace(Number(4.49423283715578976932e+307).toExponential(20)); // 0.e+327
Here's what these 4 numbers look like internally:
[Double number=8.98846567431157854072e+307 sign=1 exponent=1022
significand=11111111111111111111111111111111111111111111111111111]
[Double number=0.e+327 sign=1 exponent=1023
significand=10000000000000000000000000000000000000000000000000000]
[Double number=8.98846567431158153448e+307 sign=1 exponent=1023
significand=10000000000000000000000000000000000000000000000000001]
[Double number=0.e+327 sign=1 exponent=1022
significand=10000000000000000000000000000000000000000000000000000]
So far all three numbers that are printed incorrectly have a 0 significand (with implicit leading 1), and the exponent values of 1023 (Emax for normal numbers), 1022 (Emax - 1), or -1023 (special exponent value for ±zero and subnormal numbers, Emin - 1).
Looks like toExponent doesn't correctly handle at least these three cases
. Are there any other numbers that would print incorrectly?
Trying to check if toExponential would produce correct results with other precision values.
trace(Number(8.98846567431157953864e+307).toExponential(2)); // 0.e+309
trace(Number(8.98846567431157953864e+307).toExponential(1)); // 0.e+308
trace(Number(8.98846567431157953864e+307).toExponential(0)); // 1e+308 - less bad!
If anyone interested in my Double class I will post it. I've also written faster implementations next(x:Number) and prior(x:Number) functions which work pretty fast without messing with internal bits of the numbers.
References:
https://en.wikipedia.org/wiki/Double-precision_floating-point_format

MYSQL: Cant save 1000000 on a float field

I have a float column and I'm trying to save the value 1000000. It automatically turns it to 1e+06. How can I fix it?
To have the value returned formatted as 1000000, you can simply add integer zero to the column in the SELECT list.
SELECT mycol+0 AS mycol FROM mytable
MySQL is storing the value IEEE floating point format. (One bit for sign, a certain number of bits for the exponent, and a certain number of bits for the mantissa. This isn't really a MySQL thing, it's the standard representation for floating point values.)
As far as what's being returned, that's an issue with converting that value into string representation.
A floating point number has a large range of values. To represent the maximum value of a float (3.402823e+38) as a decimal value, that would require 38 decimal digits. The seven left most digits of the value are significant, but we'd need to add another 32 zeros/digits to indicate the position of the decimal point.
So, returning a string representation of scientific notation is a reasonable approach to returning a representation of the value.
Those two things are equivalent:
1e+06
= 1 * 10^6
= 1 * 1,000,000
= 1,000,000
It's called scientific notation (see here). mySQL uses it to display huge/tiny values, especially approximate values (see here).
You can use DOUBLE(8, 3) where 8 is the total no. of digits excluding the decimal point, and 3 is the no. of digits to follow the decimal.

Verilog floating point to binary conversion

I am trying convert a signed floating number in Verilog to a signed 24 bit binary value. For example
-0.0065 would become: 24'b100000000110101001111110
0.0901 would become: 24'b000001011100010000110010
Is this correct?
Thank you
Taking the fractional decimal 0.0901 and converting to a fixed-point value with 2 integer bits and 22 fractional bits (24 bit total):
Ruby syntax used for the maths (you can cut and paste into irb (interactive ruby) a command line tool):
i = (0.0901 * 2**20) # 377906.7904
# truncat to integer value
i = i.to_i # 377906
# Convert to string using binary (base 2)
i.to_s(2) # "1011100010000110010"
To add the leading zeros (24 bit length), right justify and pad with 0's
i.to_s(2).rjust(24, '0') # "000001011100010000110010"
# Convert to Hex (base 16)
i.to_s(16) # "5c432"
Signed numbers are a bit more problematic, easiest way is to calculate positive value then perform twos complement :
(0.0065 * 2**22).to_i.to_s(2).rjust(24, '0')
=> "000000000110101001111110"
Twos complement
"000000000110101001111110"
"111111111001010110000001" # Ones complement (bit invert)
"111111111001010110000001" + 1
"111111111001010110000010" #Twos complement
You had 24'b100000000110101001111110 which is just the positive number with the MSB set to 1 which is not how signed numbers normally work. The format you have used is Sign Magnitude, but you can not just feed that into a multiplier (as per your previous question).
NB: I have also skipped over the quantisation effect of converting to fixed point. Your coefficient when scaled by your fractional bit was 377906.7904. but we just take the integer part giving you an error of 0.7904 which will may effect your filter performance.

Regarding TCL format command

% format %2s 100
100
% format %.2s 100
10
%
%
% format %0.2s 100
10
%
I am not able to understand the difference between %2s and %.2s .
Can anyone explain me ?
TCL format command manual page specifies that format string can consist of six different parts. In this case second, third and fourth portions are of interest.
If there is a character from set [-+ 0#], they specify justification of the field, if there should be padding, sign shown of numbers, etc. 0 in the third example specifies that number should be padded with zeros instead of spaces. However, in this example there is nothing to pad.
If there is some other number without dot (2 in the first example), the number is interpreted as minimum field length and number is padded with spaces if necessary.
If there is a dot, the number after if interpreted as precision indicator and way it behaves differs depending on the other format parameters. For strings it means the maximum number of characters.
With
format %4.2s foo
you then get
fo
That is, at most two characters are printed, but the field width is at minimum 4 characters.
If you are actually trying to print a number instead of string, then the sixth (the only mandatory) field is important. "s" means "print as is". For numbers you want to use for example "d" which means decimal (integer) or "f" for floating point. Check the manual for the whole list.
With
format %4.2d 100 # Print with at least two numbers and with field width of 4 characters
you get
100
With
format %08.2f 123.45678 # Field width 8, pad with zeros, print two decimals
you get
00123.46
In the last example notice that all numbers and the dot are counted for the field length and that the number has been rounded.