Thread: I need a (64 bit * 64 bit) / 64 bit calculation function.

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    96

    I need a (64 bit * 64 bit) / 64 bit calculation function.

    I'm currently using the Windows MulDiv function to do what I need but apparently, it only works for 32 bit systems.

    I'm trying to convert the size points from the screen to any given printer.
    It'll run on a 64 bit system.

    Here's the code using MulDiv
    Code:
    long nPts = MulDiv(12, GetDeviceCaps(resource->dc, LOGPIXELSY), 72)
    72 points = 1 inch.

    This gives me the height of the screen's 12 points as the printer sees it.
    This only works for 32 bit systems.

    I need the same function, but for 64 bit computers.
    From what I gather, the problem is 64bit * 64bit = 128bit which could produce an overflow.

    I found a MulDiv64 library but can't make it compile. It compiles the files but if my code tries to call MulDiv64 it throws a compile error 'external symbol MulDiv64 unresolved'.

    I'm compiling a php7 extension, written in c. I'm using VC 2015 and a php compile nmake.

    If I could just make the MulDiv64 library work that might suffice.

    Help would be greatly appreciated.

  2. #2

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    96
    Thanks for your reply.
    It looks *to me* like most of these solutions could cause an overflow because they could end up with a 128 bit number.

    I looked at the ones using strings but I wasn't able to get a clear understanding from what was posted.
    I saw the algorithm that returns a string. Does it work?
    I notice he's using malloc. Doesn't that have to be freed at some point? Where?

    Can I do the math with all 3 numbers typed as strings and use that function?
    If so, would that take care of the overflow problem?

    I downloaded GMP but that's a really mammoth library for such a small project.

    Honestly, I was hoping to find a *small* library or just the function already written.
    I'd try to use the library I linked above if I could get it to work for me.
    If I have to I'd be willing to write it but I'd need some help.

    Again, I sure could use some help)
    Last edited by MAtkins; 07-11-2016 at 07:54 AM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > 72 points = 1 inch.
    > This gives me the height of the screen's 12 points as the printer sees it.
    > This only works for 32 bit systems.
    So what are the practical numeric ranges?

    I mean, 900 miles @ 72 points per inch wouldn't overflow a 32 bit number.

    Even the widest printers and screens are at most a few feet.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Feb 2011
    Posts
    96
    Thanks Salem. Yea I'm thinking I've started chasing a wild goose here.

    I'm trying to print text using Windows GDI.
    I need to specify the font size in points (ie Arial 12 point). The GDI prints it in 'logical units'.
    So far, the font is too small.

    Microsoft's documentation on their GDI CreateFont function suggests that we use:
    nHeight = -MulDiv(PointSize, GetDeviceCaps(hDC, LOGPIXELSY), 72); The font width is determined by the default, given Y for the given font.
    The explanation for MulDiv is "Multiplies two 32-bit values and then divides the 64-bit result by a third 32-bit value. "
    Since the explanation for MulDiv indicates 32 bit values, I thought maybe the problem is that I'm on a 64 bit machine. I think I was wrong about that.

    I just learned that font 'cell' height is used if the CreateFont function is passed a positive number for height.
    If a negative number is passed, it translates it to 'absolute' value and returns the actual font height.
    nHeight = -MulDiv(PointSize, GetDeviceCaps(hDC, LOGPIXELSY), 72);
    My code didn't include the - so I'll add that and see if it works.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function calculation
    By TheChosen0ne in forum C Programming
    Replies: 19
    Last Post: 09-29-2014, 04:43 PM
  2. calculation always gives 0
    By Emma K in forum C Programming
    Replies: 18
    Last Post: 11-24-2010, 06:00 PM
  3. Tax Calculation Help
    By Cyberdine in forum C Programming
    Replies: 22
    Last Post: 04-09-2007, 11:06 PM
  4. Help With Calculation
    By WackoWolf in forum C++ Programming
    Replies: 4
    Last Post: 10-12-2005, 05:18 PM
  5. Pi Calculation
    By EvilGuru in forum C Programming
    Replies: 2
    Last Post: 05-02-2005, 04:25 AM

Tags for this Thread