/* * Copyright © 2011 Nick Bowler * * License WTFPL2: Do What The Fuck You Want To Public License, version 2. * This is free software: you are free to do what the fuck you want to. * There is NO WARRANTY, to the extent permitted by law. */ #include #include #include #include #include const char *progname = "dpicalc"; static unsigned long simple_strtoul(const char *str, const char *name, int base) { unsigned long ret; char *end; errno = 0; ret = strtoul(str, &end, base); if (errno != 0) { fprintf(stderr, "%s: %s: invalid %s: %s\n", progname, str, name, strerror(errno)); exit(EXIT_FAILURE); } else if (*end != '\0') { fprintf(stderr, "%s: %s: invalid %s: trailing garbage\n", progname, str, name); exit(EXIT_FAILURE); } return ret; } int main(int argc, char **argv) { double diagonal, ratio, phys_width; unsigned long w, h; if (argc > 0) progname = argv[0]; if (argc < 4) { fprintf(stderr, "usage: %s diagonal pixel_w pixel_h\n", progname); return EXIT_FAILURE; } diagonal = strtod(argv[1], NULL); w = simple_strtoul(argv[2], "pixel width", 10); h = simple_strtoul(argv[3], "pixel height", 10); ratio = (double) h / w; if (!isnormal(ratio)) { fprintf(stderr, "%s: nonsensical aspect ratio (%.2f)\n", progname, ratio); } phys_width = sqrt(diagonal*diagonal / (1 + ratio*ratio)); printf("%.1f\n", w / phys_width); return 0; }