Skip to content

How c trunkate the values by default? #10611

Answered by mmosca
and-sh asked this question in Q&A
Discussion options

You must be logged in to vote

they wil both end up as 0.

#include <stdio.h>
#include <stdint.h>

int main(int argc, char **argv)
{
	const uint8_t a = 20 / 100;
        const uint8_t b = 60 / 100;

	const uint8_t roundeda = (20 / 100.0) + 0.5;
	const uint8_t roundedb = (60 / 100.0) + 0.5;


	printf("a: %i b: %i roundeda: %i roundedb: %i\n", a, b, roundeda, roundedb);

	return 0;
}

Output:
a: 0 b: 0 roundeda: 0 roundedb: 1

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@and-sh
Comment options

Answer selected by and-sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants