Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Can't set value to column conditionally

The following code throws a can't calculate log of zero error even though I check for that case in x != N

DO $$
DECLARE N integer;
BEGIN
    select count(*) from mytable INTO N;
    UPDATE
        words
    SET
    measure = 
         CASE 
            WHEN x != N THEN 1 + log((N - x) / x)
            ELSE 0
         END;   
END $$;

also tried this, same error:

DO $$
DECLARE N integer;
BEGIN
    select count(*) from mytable INTO N;
    UPDATE words
    SET measure = 1 + log((N-x)/x)
    WHERE x <> N;   
END $$; 

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

I think the cause is in the "(N-x)/x" part: Since both ‘N’ and ‘x’ are integers,
the result is rounded down to the nearest integer (which is always 0 for positive ‘N-x’ and ‘x’).

BTW, this is a duplicate question:

Division ( / ) not giving my answer in postgresql

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading