Thread: son() function output range in ISO/IEC 9899:1999(E)?

  1. #1
    Registered User
    Join Date
    Dec 2013
    Posts
    46

    son() function output range in ISO/IEC 9899:1999(E)?

    sin() function output range in ISO/IEC 9899:1999(E) (OS X man page says is based this)? i think
    it is -1 - 1... but when made code which multiples result 32767
    i get bigger numbers than 32767... for example if 32000 my waveform distorts.
    i need values beetween -32767 - 32767.
    Last edited by Vanhapolle; 12-08-2013 at 06:45 PM. Reason: I fixed function name i cannot fix title anyway at least i dont know how.

  2. #2
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    An example of the code may help clarify the question.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I'm assuming you mean sin, and yes that will be between -1 and 1. I'm a bit worried that you think 32000 is bigger than 32767 though.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    If you don't ask clear questions, you won't get clear answers.

    There is no such thing as a son() function in the C standard that produces a value between -1 and 1. There is a sin() function.

    Without knowing what your code is doing, it is impossible to explain why it is producing values outside the range you expect.

    Computers are more pedantic than people. If your code is wrong, they will obey the code in their own manner, regardless of what you expect.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    Dec 2013
    Posts
    46
    and i want add this typing mistake hard found becouse somehow i read my own text i dont see mistakes. Which comes many times form this keyboard which choiced becouse is made for mac and easily available here.(=macs on keyboard
    some others are not so easily available which have better feel).

  6. #6
    Registered User
    Join Date
    Dec 2013
    Posts
    46
    and even with 30100 "distorts".
    my formula siis this
    Code:
    x=sin(y/512*pi)*31000

    and i know in this platform short is -32768 - 32767 which means it can save all numbers what i need... but this kind code produces bigger than this numbers at least when last number is bigger than 30100 i think.
    is not exact value i dont yet found exact value.
    it seems thisway when i import my sample data in 32bit format loads fine but if i download it 16bit format which it should be based file size and fact how big short is here.

  7. #7
    Registered User
    Join Date
    Dec 2013
    Posts
    46
    it seems double->signed short conversion is problem int->signed short conversion works.

  8. #8
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    What's 512?

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Vanhapolle View Post
    and even with 30100 "distorts".
    my formula siis this
    Code:
    x=sin(y/512*pi)*31000

    and i know in this platform short is -32768 - 32767
    You're going to have to back that up with actual data, because I don't believe you. (I believe short is that range, but I don't believe that the numbers in the formula are outside that range.)

  10. #10
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by tabstop View Post
    You're going to have to back that up with actual data, because I don't believe you. (I believe short is that range, but I don't believe that the numbers in the formula are outside that range.)
    I agree: they can't be if x is short; it's impossible! Even the compiler says it's silly


    Code:
    #include <stdio.h>
    #include <math.h>
    #include <limits.h>
    #include <assert.h>
    
    #define PI        3.14159265358979323846
    #define PIOVER180 0.01745329251994329577
    
    int main(void)
    {
        size_t n;
        int thingy;     // No idea what this is
        short thing;    // No idea what this is
        
        printf("SHRT_MAX = %d\n", SHRT_MAX);
        
        for (n = 0; n < 1000000; n++) {
            thing = (PI * n / 512) * 31000; // Mystery thing
            assert(thing >= SHRT_MIN && thing <= SHRT_MAX);
            
            thingy = (short)((PI * n / 512) * 31000);
            
            if (thingy > SHRT_MAX)
                printf("SOMETHING'S WRONG! thing == %d\n", thingy);
        }
    
        return 0;
    }
    Code:
    $ gcc -Wall -Wextra van.c -lm
    van.c: In function ‘main’:
    van.c:19:9: warning: comparison is always true due to limited range of data type [-Wtype-limits]
             assert(thing >= SHRT_MIN && thing <= SHRT_MAX);
             ^
    van.c:19:9: warning: comparison is always true due to limited range of data type [-Wtype-limits]

  11. #11
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Vanhapolle View Post
    Code:
    x=sin(y/512*pi)*31000
    If y is an integral type, y/512 will be rounded toward zero. That will affect the shape of results (e.g. the visual shape of the curve produced if x is plotted against y). However, sin() returns a value in [-1,1] so x is guaranteed to be in the range [-31000,31000].

    Quote Originally Posted by Vanhapolle View Post
    and i know in this platform short is -32768 - 32767 which means it can save all numbers what i need... but this kind code produces bigger than this numbers at least when last number is bigger than 30100 i think.
    Then some code other than what you have shown is causing your problem.

    For example, if some other code is molesting a pointer and overwriting x after the expression you've shown is complete, that might explain what is going on.

    Quote Originally Posted by Vanhapolle View Post
    it seems thisway when i import my sample data in 32bit format loads fine but if i download it 16bit format which it should be based file size and fact how big short is here.
    My guess is that, in converting your code from 32-bit variables/operations to 16-bit you have changed something incorrectly. In other words, a programming error in some code you haven't shown.

    Try providing a small but complete (in the sense that other people can compile/link/execute it, supply it with relevant input data, and reproduce your problem) sample of code. In the process of creating such an example, you might have an "aha!" moment and realise what the problem is. If not, then people will have a chance of helping you.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 07-07-2011, 02:33 AM
  2. C++ code in 1999
    By chen1279 in forum C++ Programming
    Replies: 6
    Last Post: 10-04-2006, 10:02 AM
  3. Strange function output.
    By Pickels in forum C Programming
    Replies: 3
    Last Post: 08-24-2006, 08:57 PM
  4. It's time to party like it's 1999
    By -KEN- in forum Party Board
    Replies: 3
    Last Post: 04-27-2004, 04:01 PM
  5. Incrementing time and calendarfrom 1999-2000
    By Karma in forum C++ Programming
    Replies: 1
    Last Post: 12-03-2001, 02:48 PM