How to type cast int to float?

Hello, how to do type casting from int to float in this query

{    
  var(func: uid(0x6)){
		norm as math(1) 
    u1_total_interests as count(recommender.User.interests)
    u1_interests as recommender.User.interests{
			~recommender.User.interests{
  			u2_total_interests as count(recommender.User.interests)
				u1_norm_total_interests as math(u1_total_interests / norm)
        u1_u2_total_intersect_interests as count(u2_total_interests @filter(uid(u1_interests)))
        u1_u2_total_union_interests as math(u1_norm_total_interests + u2_total_interests)
        divs as math(1.0 * u1_u2_total_intersect_interests / u1_u2_total_union_interests) 
        distance as math(1 - divs)
    	}
  	}
  }
          
   q(func: uid(0x6)) {
    recommender.User.id
    val(norm)            
    val(u1_total_interests)
    recommender.User.interests @filter(uid(u1_interests)) {
      recommender.Interest.id
      ~recommender.User.interests @filter(not uid(0x6)) {
        recommender.User.id
      	recommender.Interest.id
    		val(norm)            
        val(u2_total_interests) 
        val(u1_norm_total_interests)
        val(u1_u2_total_intersect_interests)
        val(u1_u2_total_union_interests)
        val(divs)
      	val(distance)
      }
    }
  }
}

because when I do the query the distance is always in integer which caused the distance to have value either 1 or 0

thank you

here is an example result when I run the query above

{
  "data": {
    "q": [
      {
        "recommender.User.id": "c3rutfb2dnce1sh6njl0",
        "val(norm)": 1,
        "val(u1_total_interests)": 5,
        "recommender.User.interests": [
          {
            "recommender.Interest.id": "doodle",
            "~recommender.User.interests": [
              {
                "recommender.User.id": "c49049b2dncb3h8305lg",
                "val(u2_total_interests)": 3,
                "val(u1_norm_total_interests)": 5,
                "val(u1_u2_total_union_interests)": 8,
                "val(divs)": 0,
                "val(distance)": 1
              },
              {
                "recommender.User.id": "c49058j2dncb3h8305q0",
                "val(u2_total_interests)": 3,
                "val(u1_norm_total_interests)": 5,
                "val(u1_u2_total_union_interests)": 8,
                "val(divs)": 0,
                "val(distance)": 1
              }
            ]
          },
          {
            "recommender.Interest.id": "poodle",
            "~recommender.User.interests": [
              {
                "recommender.User.id": "c49049b2dncb3h8305lg",
                "val(u2_total_interests)": 3,
                "val(u1_norm_total_interests)": 5,
                "val(u1_u2_total_union_interests)": 8,
                "val(divs)": 0,
                "val(distance)": 1
              },
              {
                "recommender.User.id": "c4904ir2dncb3h8305n0",
                "val(u2_total_interests)": 3,
                "val(u1_norm_total_interests)": 5,
                "val(u1_u2_total_union_interests)": 8,
                "val(divs)": 0,
                "val(distance)": 1
              },
              {
                "recommender.User.id": "c49058j2dncb3h8305q0",
                "val(u2_total_interests)": 3,
                "val(u1_norm_total_interests)": 5,
                "val(u1_u2_total_union_interests)": 8,
                "val(divs)": 0,
                "val(distance)": 1
              }
            ]
          },
          {
            "recommender.Interest.id": "vienetta",
            "~recommender.User.interests": [
              {
                "recommender.User.id": "c4904ir2dncb3h8305n0",
                "val(u2_total_interests)": 3,
                "val(u1_norm_total_interests)": 5,
                "val(u1_u2_total_union_interests)": 8,
                "val(divs)": 0,
                "val(distance)": 1
              },
              {
                "recommender.User.id": "c4904qb2dncb3h8305og",
                "val(u2_total_interests)": 3,
                "val(u1_norm_total_interests)": 5,
                "val(u1_u2_total_union_interests)": 8,
                "val(divs)": 0,
                "val(distance)": 1
              }
            ]
          },
          {
            "recommender.Interest.id": "bables",
            "~recommender.User.interests": [
              {
                "recommender.User.id": "c4904ir2dncb3h8305n0",
                "val(u2_total_interests)": 3,
                "val(u1_norm_total_interests)": 5,
                "val(u1_u2_total_union_interests)": 8,
                "val(divs)": 0,
                "val(distance)": 1
              },
              {
                "recommender.User.id": "c4904qb2dncb3h8305og",
                "val(u2_total_interests)": 3,
                "val(u1_norm_total_interests)": 5,
                "val(u1_u2_total_union_interests)": 8,
                "val(divs)": 0,
                "val(distance)": 1
              },
              {
                "recommender.User.id": "c49058j2dncb3h8305q0",
                "val(u2_total_interests)": 3,
                "val(u1_norm_total_interests)": 5,
                "val(u1_u2_total_union_interests)": 8,
                "val(divs)": 0,
                "val(distance)": 1
              }
            ]
          },
          {
            "recommender.Interest.id": "piglet",
            "~recommender.User.interests": [
              {
                "recommender.User.id": "c49049b2dncb3h8305lg",
                "val(u2_total_interests)": 3,
                "val(u1_norm_total_interests)": 5,
                "val(u1_u2_total_union_interests)": 8,
                "val(divs)": 0,
                "val(distance)": 1
              },
              {
                "recommender.User.id": "c4904qb2dncb3h8305og",
                "val(u2_total_interests)": 3,
                "val(u1_norm_total_interests)": 5,
                "val(u1_u2_total_union_interests)": 8,
                "val(divs)": 0,
                "val(distance)": 1
              }
            ]
          }
        ]
      }
    ]
  },
  "extensions": {
    "server_latency": {
      "parsing_ns": 250300,
      "processing_ns": 6891300,
      "encoding_ns": 290100,
      "assign_timestamp_ns": 1659600,
      "total_ns": 9722100
    },
    "txn": {
      "start_ts": 10638
    },
    "metrics": {
      "num_uids": {
        "": 13,
        "_total": 50,
        "recommender.Interest.id": 9,
        "recommender.User.id": 5,
        "recommender.User.interests": 8,
        "u2_total_interests": 5,
        "~recommender.User.interests": 10
      }
    }
  }
}

Welcome Antony!
Could you please try with the following?
divs as math(1.0 * u1_u2_total_intersect_interests / ( u1_u2_total_union_interests * 1.0 ) )

hello Anand, sure I tried it just now but unfortunately it’s still have the same result

Hi Antony,
Could you please try this query and let me know the result? Put any condition in the root function (like has(name)) which will give you at least one record in the result.

{
  q(func: has(name), first: 1){
    uid
    u1_u2_total_intersect_interests as math(3)
    u1_u2_total_union_interests as math(8)
    divs : math( u1_u2_total_intersect_interests / ( u1_u2_total_union_interests * 1.0) )
  }
}

The response I get is:

{
  "data": {
    "q": [
      {
        "uid": "0x3f",
        "val(u1_u2_total_intersect_interests)": 3,
        "val(u1_u2_total_union_interests)": 8,
        "divs": 0.375
      }
    ]
  }
}

hello Anand,
I got the same result as yours with the query above

1 Like

Hi @crustaceanix ,
Could you please share the schema you are using?

Hello Anand,
Sure here it is

<dgraph.drop.op>: string .
<dgraph.graphql.p_query>: string @index(sha256) .
<dgraph.graphql.schema>: string .
<dgraph.graphql.xid>: string @index(exact) @upsert .
<recommender.Interest.id>: string @index(exact) @upsert .
<recommender.Interest.version>: int @index(int) .
<recommender.User.added_at>: datetime @index(year) .
<recommender.User.id>: string @index(exact) @upsert .
<recommender.User.interests>: [uid] @count @reverse .
<recommender.User.updated_at>: datetime @index(year) .
<recommender.User.version>: int @index(int) .
type <dgraph.graphql> {
	dgraph.graphql.schema
	dgraph.graphql.xid
}
type <dgraph.graphql.persisted_query> {
	dgraph.graphql.p_query
}
type <recommender.Interest.> {
	recommender.Interest.id
	recommender.Interest.version
}
type <recommender.User> {
	recommender.User.id
	recommender.User.added_at
	recommender.User.updated_at
	recommender.User.version
	recommender.User.interests
}

Hi @anand
any update regarding this issue?
thank you

Hi @crustaceanix ,
This is not a casting issue. If you notice the query output, you will see that variable u1_u2_total_intersect_interests doesn’t appear, even though you are querying for it. The variable is not available to be queried (might be a bug in the way the query has been written). This means that any other dependent variable, such as divs and others will evaluate to 0.

Please review your query.

ah I see my bad,
I didn’t see that the value of u1_u2_total_intersect_interests doesn’t appear
I’ve fixed the query with this new query

{    
	var(func: uid(0x6)){
		recommender.User.interests{
    	~recommender.User.interests{
  			u2_total_interests as count(recommender.User.interests)
  		}
  	}
  }
  
  var(func: uid(0x6)){
		norm as math(1) 
    u1_total_interests as count(recommender.User.interests)
    u1_interests as recommender.User.interests{
			~recommender.User.interests{
  			# u2_total_interests as count(recommender.User.interests)
				u1_norm_total_interests as math(u1_total_interests / norm)
        u1_u2_total_intersect_interests as count(recommender.User.interests @filter(uid(u1_interests)))
        u1_u2_total_union_interests as math(u1_norm_total_interests + u2_total_interests)
        divs as math(1.0 * u1_u2_total_intersect_interests / ( u1_u2_total_union_interests * 1.0 )) 
        distance as math(1 - divs)
    	}
  	}
  }
          
   q(func: uid(0x6)) {
    recommender.User.id
    val(norm)            
    val(u1_total_interests)
    recommender.User.interests @filter(uid(u1_interests)) {
      recommender.Interest.id
      ~recommender.User.interests @filter(not uid(0x6)) {
        recommender.User.id
      	recommender.Interest.id
    		val(norm)            
        val(u2_total_interests) 
        val(u1_norm_total_interests)
        val(u1_u2_total_intersect_interests)
        val(u1_u2_total_union_interests)
        val(divs)
      	val(distance)
      }
    }
  }
}

and now it’s working properly

{
  "data": {
    "q": [
      {
        "recommender.User.id": "c3rutfb2dnce1sh6njl0",
        "val(norm)": 1,
        "val(u1_total_interests)": 5,
        "recommender.User.interests": [
          {
            "recommender.Interest.id": "doodle",
            "~recommender.User.interests": [
              {
                "recommender.User.id": "c49049b2dncb3h8305lg",
                "val(u2_total_interests)": 3,
                "val(u1_norm_total_interests)": 5,
                "val(u1_u2_total_intersect_interests)": 3,
                "val(u1_u2_total_union_interests)": 8,
                "val(divs)": 0.375,
                "val(distance)": 0.625
              },
              {
                "recommender.User.id": "c49058j2dncb3h8305q0",
                "val(u2_total_interests)": 3,
                "val(u1_norm_total_interests)": 5,
                "val(u1_u2_total_intersect_interests)": 3,
                "val(u1_u2_total_union_interests)": 8,
                "val(divs)": 0.375,
                "val(distance)": 0.625
              }
            ]
          },
          {
            "recommender.Interest.id": "poodle",
            "~recommender.User.interests": [
              {
                "recommender.User.id": "c49049b2dncb3h8305lg",
                "val(u2_total_interests)": 3,
                "val(u1_norm_total_interests)": 5,
                "val(u1_u2_total_intersect_interests)": 3,
                "val(u1_u2_total_union_interests)": 8,
                "val(divs)": 0.375,
                "val(distance)": 0.625
              },
              {
                "recommender.User.id": "c4904ir2dncb3h8305n0",
                "val(u2_total_interests)": 3,
                "val(u1_norm_total_interests)": 5,
                "val(u1_u2_total_intersect_interests)": 3,
                "val(u1_u2_total_union_interests)": 8,
                "val(divs)": 0.375,
                "val(distance)": 0.625
              },
              {
                "recommender.User.id": "c49058j2dncb3h8305q0",
                "val(u2_total_interests)": 3,
                "val(u1_norm_total_interests)": 5,
                "val(u1_u2_total_intersect_interests)": 3,
                "val(u1_u2_total_union_interests)": 8,
                "val(divs)": 0.375,
                "val(distance)": 0.625
              }
            ]
          },
          {
            "recommender.Interest.id": "vienetta",
            "~recommender.User.interests": [
              {
                "recommender.User.id": "c4904ir2dncb3h8305n0",
                "val(u2_total_interests)": 3,
                "val(u1_norm_total_interests)": 5,
                "val(u1_u2_total_intersect_interests)": 3,
                "val(u1_u2_total_union_interests)": 8,
                "val(divs)": 0.375,
                "val(distance)": 0.625
              },
              {
                "recommender.User.id": "c4904qb2dncb3h8305og",
                "val(u2_total_interests)": 3,
                "val(u1_norm_total_interests)": 5,
                "val(u1_u2_total_intersect_interests)": 3,
                "val(u1_u2_total_union_interests)": 8,
                "val(divs)": 0.375,
                "val(distance)": 0.625
              }
            ]
          },
          {
            "recommender.Interest.id": "bables",
            "~recommender.User.interests": [
              {
                "recommender.User.id": "c4904ir2dncb3h8305n0",
                "val(u2_total_interests)": 3,
                "val(u1_norm_total_interests)": 5,
                "val(u1_u2_total_intersect_interests)": 3,
                "val(u1_u2_total_union_interests)": 8,
                "val(divs)": 0.375,
                "val(distance)": 0.625
              },
              {
                "recommender.User.id": "c4904qb2dncb3h8305og",
                "val(u2_total_interests)": 3,
                "val(u1_norm_total_interests)": 5,
                "val(u1_u2_total_intersect_interests)": 3,
                "val(u1_u2_total_union_interests)": 8,
                "val(divs)": 0.375,
                "val(distance)": 0.625
              },
              {
                "recommender.User.id": "c49058j2dncb3h8305q0",
                "val(u2_total_interests)": 3,
                "val(u1_norm_total_interests)": 5,
                "val(u1_u2_total_intersect_interests)": 3,
                "val(u1_u2_total_union_interests)": 8,
                "val(divs)": 0.375,
                "val(distance)": 0.625
              }
            ]
          },
          {
            "recommender.Interest.id": "piglet",
            "~recommender.User.interests": [
              {
                "recommender.User.id": "c49049b2dncb3h8305lg",
                "val(u2_total_interests)": 3,
                "val(u1_norm_total_interests)": 5,
                "val(u1_u2_total_intersect_interests)": 3,
                "val(u1_u2_total_union_interests)": 8,
                "val(divs)": 0.375,
                "val(distance)": 0.625
              },
              {
                "recommender.User.id": "c4904qb2dncb3h8305og",
                "val(u2_total_interests)": 3,
                "val(u1_norm_total_interests)": 5,
                "val(u1_u2_total_intersect_interests)": 3,
                "val(u1_u2_total_union_interests)": 8,
                "val(divs)": 0.375,
                "val(distance)": 0.625
              }
            ]
          }
        ]
      }
    ]
  },
  "extensions": {
    "server_latency": {
      "parsing_ns": 1690600,
      "processing_ns": 8530900,
      "encoding_ns": 147100,
      "assign_timestamp_ns": 2196600,
      "total_ns": 13741500
    },
    "txn": {
      "start_ts": 11427
    },
    "metrics": {
      "num_uids": {
        "": 19,
        "_total": 62,
        "recommender.Interest.id": 9,
        "recommender.User.id": 5,
        "recommender.User.interests": 14,
        "~recommender.User.interests": 15
      }
    }
  }
}

thank you so much @anand :+1:

1 Like