{"id":2363,"date":"2015-05-29T22:27:22","date_gmt":"2015-05-29T21:27:22","guid":{"rendered":"http:\/\/roboblog.fatal-fury.de\/?p=2363"},"modified":"2015-05-29T22:28:21","modified_gmt":"2015-05-29T21:28:21","slug":"warning-conversion-to-float-from-int-may-alter-its-value-wconversion","status":"publish","type":"post","link":"http:\/\/roboblog.fatal-fury.de\/?p=2363","title":{"rendered":"warning: conversion to 'float' from 'int' may alter its value [-Wconversion] (Bug of the day 7)"},"content":{"rendered":"<p>Another bug of the day :D<\/p>\n<p>You can do the following<\/p>\n<p>float var = float(vari); \/\/ yes I know I'm loosing precision<br \/>\nint var = int(std::floor(varf)) \/\/ yes I know interger can overflow<br \/>\nint var = qFloor(varf) \/\/ Qt version<\/p>\n<p>Same for double.<\/p>\n<p>warning: comparison between signed and unsigned integer expressions [-Wsign-compare]<\/p>\n<p>Tricky one. Usually don't use unsigned types. The world is full of negative value. Use signed types.<br \/>\nExcept for..<br \/>\nArray index. To access the full 32\/64bit address space, one must use unsigned type. <\/p>\n<p>Let variable data be a pointer\/array.<br \/>\ndata[i] is the same as data+i<br \/>\nNow variable i should have the same wide as data. Which is size_t. Type size_t is 32bit wide on 32bit system and 64bit otherwise.<br \/>\nNew rule: Always use size_t for index variable while accessing data. Don't use int.<\/p>\n<p>Type size_t is an unsiged type.<br \/>\nNew rule: Never count backwards in for loops while accessing data.<\/p>\n<p>Consider this endless loop<br \/>\nfor(size_t i=data.size(); i >= 0; i--)<\/p>\n<p>If i == 0 and i-- then i == 2^32-1 which is > 0. Damn.<br \/>\nThere are a lot of solutions. Bad one and really bad ones. Here is my perfect one: You count forward. That is really easy and everybody understands what is going one and there are no surprises.<br \/>\nIn the next line you subtract n-i-1 and volia. Perfect.<\/p>\n<p>for(size_t ri=0; ri < data.size(); ri++) {\n  size_t i = data.size() - ri - 1;\n\nI named the new loop variabl ri for reverse iterator. Don't forget to add -1. Arrays start by 0.\nYou can of couse use std::reverse_iterator But hey, I love for loops!\n<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Another bug of the day :D You can do the following float var = float(vari); \/\/ yes I know I'm loosing precision int var = int(std::floor(varf)) \/\/ yes I know interger can overflow int var = qFloor(varf) \/\/ Qt version Same for double. warning: comparison between signed and unsigned integer expressions [-Wsign-compare] Tricky one. Usually [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[14,17,12],"class_list":["post-2363","post","type-post","status-publish","format-standard","hentry","category-allgemein","tag-c","tag-cpp","tag-qt"],"_links":{"self":[{"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/posts\/2363","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2363"}],"version-history":[{"count":4,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/posts\/2363\/revisions"}],"predecessor-version":[{"id":2367,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/posts\/2363\/revisions\/2367"}],"wp:attachment":[{"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2363"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2363"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2363"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}