mirror of
				https://github.com/stedolan/jq.git
				synced 2024-05-11 05:55:39 +00:00 
			
		
		
		
	Add sqrt operator
This commit is contained in:
		
							
								
								
									
										11
									
								
								builtin.c
									
									
									
									
									
								
							
							
						
						
									
										11
									
								
								builtin.c
									
									
									
									
									
								
							@@ -76,6 +76,15 @@ static jv f_floor(jv input) {
 | 
				
			|||||||
  return ret;
 | 
					  return ret;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static jv f_sqrt(jv input) {
 | 
				
			||||||
 | 
					  if (jv_get_kind(input) != JV_KIND_NUMBER) {
 | 
				
			||||||
 | 
					    return type_error(input, "has no square root");
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  jv ret = jv_number(sqrt(jv_number_value(input)));
 | 
				
			||||||
 | 
					  jv_free(input);
 | 
				
			||||||
 | 
					  return ret;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static jv f_negate(jv input) {
 | 
					static jv f_negate(jv input) {
 | 
				
			||||||
  if (jv_get_kind(input) != JV_KIND_NUMBER) {
 | 
					  if (jv_get_kind(input) != JV_KIND_NUMBER) {
 | 
				
			||||||
    return type_error(input, "cannot be negated");
 | 
					    return type_error(input, "cannot be negated");
 | 
				
			||||||
@@ -491,6 +500,7 @@ static jv f_error(jv input, jv msg) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
static const struct cfunction function_list[] = {
 | 
					static const struct cfunction function_list[] = {
 | 
				
			||||||
  {(cfunction_ptr)f_floor, "_floor", 1},
 | 
					  {(cfunction_ptr)f_floor, "_floor", 1},
 | 
				
			||||||
 | 
					  {(cfunction_ptr)f_sqrt, "_sqrt", 1},
 | 
				
			||||||
  {(cfunction_ptr)f_plus, "_plus", 3},
 | 
					  {(cfunction_ptr)f_plus, "_plus", 3},
 | 
				
			||||||
  {(cfunction_ptr)f_negate, "_negate", 1},
 | 
					  {(cfunction_ptr)f_negate, "_negate", 1},
 | 
				
			||||||
  {(cfunction_ptr)f_minus, "_minus", 3},
 | 
					  {(cfunction_ptr)f_minus, "_minus", 3},
 | 
				
			||||||
@@ -576,6 +586,7 @@ static const char* const jq_builtins[] = {
 | 
				
			|||||||
  "def max_by(f): _max_by_impl(map([f]));",
 | 
					  "def max_by(f): _max_by_impl(map([f]));",
 | 
				
			||||||
  "def min_by(f): _min_by_impl(map([f]));",
 | 
					  "def min_by(f): _min_by_impl(map([f]));",
 | 
				
			||||||
  "def floor: _floor;",
 | 
					  "def floor: _floor;",
 | 
				
			||||||
 | 
					  "def sqrt: _sqrt;",
 | 
				
			||||||
  "def add: reduce .[] as $x (null; . + $x);",
 | 
					  "def add: reduce .[] as $x (null; . + $x);",
 | 
				
			||||||
  "def del(f): delpaths([path(f)]);",
 | 
					  "def del(f): delpaths([path(f)]);",
 | 
				
			||||||
  "def _assign(paths; value): value as $v | reduce path(paths) as $p (.; setpath($p; $v));",
 | 
					  "def _assign(paths; value): value as $v | reduce path(paths) as $p (.; setpath($p; $v));",
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -633,6 +633,16 @@ sections:
 | 
				
			|||||||
            input: '3.14159'
 | 
					            input: '3.14159'
 | 
				
			||||||
            output: ['3']
 | 
					            output: ['3']
 | 
				
			||||||
            
 | 
					            
 | 
				
			||||||
 | 
					      - title: `sqrt`
 | 
				
			||||||
 | 
					        body: |
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					          The `sqrt` function returns the square root of its numeric input.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        examples:
 | 
				
			||||||
 | 
					          - program: 'sqrt'
 | 
				
			||||||
 | 
					            input: '9'
 | 
				
			||||||
 | 
					            output: ['3']
 | 
				
			||||||
 | 
					            
 | 
				
			||||||
      - title: `tonumber`
 | 
					      - title: `tonumber`
 | 
				
			||||||
        body: |
 | 
					        body: |
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -366,6 +366,10 @@ null
 | 
				
			|||||||
[-1.1,1.1,1.9]
 | 
					[-1.1,1.1,1.9]
 | 
				
			||||||
[-2, 1, 1]
 | 
					[-2, 1, 1]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					[.[]|sqrt]
 | 
				
			||||||
 | 
					[4,9]
 | 
				
			||||||
 | 
					[2,3]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def f(x): x | x; f([.], . + [42])
 | 
					def f(x): x | x; f([.], . + [42])
 | 
				
			||||||
[1,2,3]
 | 
					[1,2,3]
 | 
				
			||||||
[[[1,2,3]]]
 | 
					[[[1,2,3]]]
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user