Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
felipensp committed Jan 2, 2025
1 parent 32c3acb commit 6dcfa64
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 16 deletions.
30 changes: 15 additions & 15 deletions vlib/math/stats/stats_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -400,15 +400,15 @@ fn test_covariance() {
mut data0 := [10.0, 4.45, 5.9, 2.7]
mut data1 := [5.0, 14.45, -15.9, 22.7]
mut o := stats.covariance(data0, data1)
assert math.alike(o, -17.37078207731247)
assert math.alike(o, -17.37078125)
data0 = [-3.0, 67.31, 4.4, 1.89]
data1 = [5.0, 77.31, 44.4, 11.89]
o = stats.covariance(data0, data1)
assert math.alike(o, 740.0695419311523)
assert math.alike(o, 740.06955)
data0 = [12.0, 7.88, 76.122, 54.83]
data1 = [2.0, 5.88, 7.122, 5.83]
o = stats.covariance(data0, data1)
assert math.alike(o, 36.65028190612793)
assert math.alike(o, 36.650282000000004)

// test for int, i64, f32 array
data0_int := [1, 2, 3, 1]
Expand All @@ -428,13 +428,13 @@ fn test_covariance() {
fn test_lag1_autocorrelation() {
mut data := [10.0, 4.45, 5.9, 2.7]
mut o := stats.lag1_autocorrelation(data)
assert math.alike(o, -0.5542285495255542)
assert math.alike(o, -0.5542285481446095)
data = [-3.0, 67.31, 4.4, 1.89]
o = stats.lag1_autocorrelation(data)
assert math.alike(o, -0.5102510688760657)
assert math.alike(o, -0.5102510654033415)
data = [12.0, 7.88, 76.122, 54.83]
o = stats.lag1_autocorrelation(data)
assert math.alike(o, 0.10484451223403927)
assert math.alike(o, 0.10484450460892072)

// test for int, i64, f32 array
assert stats.lag1_autocorrelation[int]([1, 2, 3, 1]) == 0
Expand All @@ -446,13 +446,13 @@ fn test_lag1_autocorrelation() {
fn test_kurtosis() {
mut data := [10.0, 4.45, 5.9, 2.7]
mut o := stats.kurtosis(data)
assert math.alike(o, -1.0443214689384779)
assert math.alike(o, -1.0443212849233845)
data = [-3.0, 67.31, 4.4, 1.89]
o = stats.kurtosis(data)
assert math.alike(o, -0.688495594786176)
assert math.alike(o, -0.6884953374814851)
data = [12.0, 7.88, 76.122, 54.83]
o = stats.kurtosis(data)
assert math.alike(o, -1.7323772574195067)
assert math.alike(o, -1.7323772836921467)

// test for int, i64, f32 array
assert stats.kurtosis[int]([1, 2, 3, 1]) == 1
Expand All @@ -464,13 +464,13 @@ fn test_kurtosis() {
fn test_skew() {
mut data := [10.0, 4.45, 5.9, 2.7]
mut o := stats.skew(data)
assert math.alike(o, 0.5754020379048158)
assert math.alike(o, 0.5754021106320453)
data = [-3.0, 67.31, 4.4, 1.89]
o = stats.skew(data)
assert math.alike(o, 1.1248732608899568)
assert math.alike(o, 1.1248733711136492)
data = [12.0, 7.88, 76.122, 54.83]
o = stats.skew(data)
assert math.alike(o, 0.19007917421924964)
assert math.alike(o, 0.19007911706827735)

// test for int, i64, f32 array
assert stats.skew[int]([1, 2, 3, 1]) == 2
Expand All @@ -484,13 +484,13 @@ fn test_quantile() {

mut data := [2.7, 4.45, 5.9, 10.0]
mut o := stats.quantile(data, 0.1)!
assert math.alike(o, 3.225000020861626)
assert math.alike(o, 3.2249999046325684)
data = [-3.0, 1.89, 4.4, 67.31]
o = stats.quantile(data, 0.2)!
assert math.alike(o, -0.06599988341331486)
assert math.alike(o, -0.06600001454353333)
data = [7.88, 12.0, 54.83, 76.122]
o = stats.quantile(data, 0.3)!
assert math.alike(o, 11.587999901771546)
assert math.alike(o, 11.588000297546387)

stats.quantile(data, -0.3) or { assert err.msg() == 'index out of range' }

Expand Down
5 changes: 5 additions & 0 deletions vlib/v/checker/assign.v
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,11 @@ fn (mut c Checker) assign_stmt(mut node ast.AssignStmt) {
left.obj.ct_type_var = .field_var
left.obj.typ = c.comptime.comptime_for_field_type
}
} else if mut right is ast.InfixExpr {
right_ct_var := c.comptime.get_ct_type_var(right.left)
if right_ct_var in [.generic_var, .generic_param] {
left.obj.ct_type_var = right_ct_var
}
} else if mut right is ast.Ident && right.obj is ast.Var
&& right.or_expr.kind == .absent {
right_obj_var := right.obj as ast.Var
Expand Down
8 changes: 8 additions & 0 deletions vlib/v/gen/c/assign.v
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,14 @@ fn (mut g Gen) assign_stmt(node_ ast.AssignStmt) {
left.obj.typ = var_type
g.assign_ct_type = var_type
}
} else if val is ast.InfixExpr {
ctyp := g.unwrap_generic(g.type_resolver.get_type(val.left))
if ctyp != ast.void_type {
var_type = ctyp
val_type = var_type
left.obj.typ = var_type
g.assign_ct_type = var_type
}
}
is_auto_heap = left.obj.is_auto_heap
}
Expand Down
2 changes: 1 addition & 1 deletion vlib/v/type_resolver/generic_resolver.v
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ pub fn (mut t TypeResolver) resolve_args(cur_fn &ast.FnDecl, func &ast.Fn, mut n
comptime_args[k] = m.return_type
}
}
} else if mut call_arg.expr is ast.CastExpr {
} else if mut call_arg.expr is ast.CastExpr && call_arg.expr.typ.has_flag(.generic) {
cparam_type_sym := t.table.sym(t.resolver.unwrap_generic(call_arg.expr.typ))
param_typ_sym := t.table.sym(param_typ)
if param_typ_sym.kind == .map && cparam_type_sym.info is ast.Map {
Expand Down
19 changes: 19 additions & 0 deletions vlib/v/type_resolver/type_resolver.v
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,19 @@ pub fn (mut t TypeResolver) get_type_or_default(node ast.Expr, default_typ ast.T
return t.get_type_or_default(node.left, default_typ)
}
}
ast.IndexExpr {
if node.left is ast.Ident && node.left.ct_expr {
ctyp := t.get_type(node)
if ctyp != ast.void_type {
return ctyp
}
}
}
ast.CastExpr {
if node.typ.has_flag(.generic) {
return t.resolver.unwrap_generic(node.typ)
}
}
else {
return default_typ
}
Expand Down Expand Up @@ -184,6 +197,12 @@ pub fn (mut t TypeResolver) get_type(node ast.Expr) ast.Type {
nltype := t.get_type(node.left)
nltype_unwrapped := t.resolver.unwrap_generic(nltype)
return t.table.value_type(nltype_unwrapped)
} else if node is ast.ParExpr && t.info.is_comptime(node.expr) {
return t.get_type(node.expr)
} else if node is ast.InfixExpr && t.info.is_comptime(node.left) {
return t.get_type(node.left)
} else if node is ast.CastExpr && node.typ.has_flag(.generic) {
return t.resolver.unwrap_generic(node.typ)
}
return ast.void_type
}

0 comments on commit 6dcfa64

Please sign in to comment.