计算C中的正弦曲线

问题描述

我想获得一个从正弦曲线到0到指定高度(在我的情况下为40)之间移动的周期值。

但是我搞砸了,因为我的价值一直上升到79,而不是预期的40。我在做什么错了?

这是我的尝试

#include <math.h>

    #define degToRad(angleInDegrees) ((angleInDegrees)*M_PI / 180.0)
    
    int main()
    {  
        int height = 40;
        int i = 0;
        while (1) {
    
            int value = height + sin(degToRad(i / 2 + 1)) * height;
            printf("val = %i\n",value);
            i++;
        }
        return 0;
    }

解决方法

则曲线的振幅将为height / 2,而不是height;只需替换

int value = height + sin(degToRad(i / 2 + 1)) * height;

使用

int value = height / 2 + sin(degToRad(i / 2 + 1)) * height / 2;

记住这一点的好方法是sin x始终在[-1,1]范围内。

,

直接的解决方法是将波幅除以2 @Eric Postpischil

import time

from pubnub.pubnub import PubNub
from pubnub.pnconfiguration import PNConfiguration
from pubnub.callbacks import SubscribeCallback
from backend.blockchain.block import Block

pnconfig = PNConfiguration()
pnconfig.suscribe_key = 'sub-c-6d0fe192-dee4-11ea-9b19-...'
pnconfig.publish_key = 'pub-c-c3553c68-bf24-463c-ae43-...'

CHANNELS = {
'TEST': 'TEST','BLOCK': 'BLOCK'
}

class Listener(SubscribeCallback):
    def __init__(self,blockchain):
        self.blockchain = blockchain

        def message(self,pubnub,message_object):
            print('\n-- Channel: {message_object.channel} | Message: {message_object.message}')

            if message_object.channel == CHANNELS['BLOCK']:
                block = Block.from_json(message_object.message)
                potential_chain = self.blockchain.chain[:]
                potential_chain.append(block)

                try:
                    self.blockchain.replace_chain(potential_chain)
                    print('\n -- Successfully replaced the local chain')
                except Exception as e:
                    print('\n -- Did not replace chain: {e}')

class PubSub():
    """
    Handles the publish/subscribe layer of the application.
    Provides communication between the nodes of the blockchain network.
    """
    def __init__(self,blockchain):
        self.pubnub = PubNub(pnconfig)
        self.pubnub.subscribe().channels(CHANNELS.values()).execute()
        self.pubnub.add_listener(Listener(blockchain))

        def publish(self,channel,message):
            """
            Publish the message object to the channel.
            """
            self.pubnub.publish().channel(channel).message(message).sync()

        def broadcast_block(self,block):
            """
            Broadcast a block object to all nodes.
            """
            self.publish(CHANNELS['BLOCK'],block.to_json())

            def main():
                pubsub = PubSub()

                time.sleep(1)

                pubsub.publish(CHANNELS['TEST'],{ 'foo': 'bar' })

                if __name__ == '__main__':
                    main()

,并在// int value = height + sin(degToRad(i / 2 + 1)) * height; int value = height + sin(degToRad(i / 2 + 1)) * height)/2; 部门中使用浮点数学。 @bruno


我希望使用四舍五入而不是截断(OP的代码所做的事情)从浮点到i/2会得到更可接受的结果。

int

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...