Getting the relationship between two numbers

This is a discussion on Getting the relationship between two numbers within the C# Programming forums, part of the General Programming Boards category; I have two numbers, that I have got from angle, using Cos and Sin. If I add the numbers together, ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    98

    Getting the relationship between two numbers

    I have two numbers, that I have got from angle, using Cos and Sin. If I add the numbers together, they generally range from about 0.6 to about 1.6. How can I write a method that makes the numbers larger or smaller, so that they add up to a given number, for example 1, while still holding their relationship to each other?

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,171
    Huh? I don't understand what you mean. Especially about "while still holding their relationship to each other".
    And how to make a number larger or smaller? I'm guessing it's related to the above somehow.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    98
    Lets say, for example, the two numbers, X and Y, are 0.6 and 0.9. If added together they equal 1.5. Now how would I go about lowering both numbers, while keeping their ratio, till they are both equal to 1, or any given number?

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,171
    I see. So an equation, huh?
    Oops. Bad solution.
    Last edited by Elysia; 01-29-2008 at 11:42 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    16
    Quote Originally Posted by Elysia View Post
    I see. So an equation, huh?
    One way would be to do this:
    Code:
    float x = 0.6f;
    float y = 0.9f;
    float total = x + y;
    float rest = total - 1.0f;
    float subtract = rest / 2;
    x -= subtract;
    y -= sibtract;
    Wouldn't it?
    No, that would take 0.25 from 0.6 and 0.25 from 0.9, which is obviously not a relative result.

    Code:
    float x = 0.6f;
    float y = 0.9f;
    
    float x2 = x / (x + y);
    float y2 = y / (x + y);

  6. #6
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Quote Originally Posted by Elysia
    Wouldn't it?
    I don't think so.

    You want ratios.

    .6 + .9 = 1.5, and you want them to add up to 1.0.

    Read another way, you have .60 and .90 and they add up to 1.50. If we multiply by 100, you have

    60% + 90% = 150%, but you want them to add up to 100%.

    So, figure out what % .6 is of 1.5, and that is 40%.

    .6 / 1.5 = 40.

    Checking our math,

    .9 / 1.5 = 60.

    40% + 60% = 100%, so the answer is .4 and .6.

    Todd
    Last edited by Dino; 01-29-2008 at 11:25 AM.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,171
    Oops, think I missed the "keep ratios" words there.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  8. #8
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    I don't know C#, but in general you want something like this (where limit in your example would be 1.0).
    Code:
        convert = limit / (x + y);
        x *= convert;
        y *= convert;

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    98
    Quote Originally Posted by oogabooga View Post
    I don't know C#, but in general you want something like this (where limit in your example would be 1.0).
    Code:
        convert = limit / (x + y);
        x *= convert;
        y *= convert;
    Thanks, that worked. It took me a while to figure out that I need absolute values, but I got it working. Once again, thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  2. Replies: 4
    Last Post: 03-03-2003, 02:52 PM
  3. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 10:15 AM
  4. Homework help
    By Jigsaw in forum C++ Programming
    Replies: 2
    Last Post: 03-06-2002, 04:56 PM
  5. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 05:38 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21