Shop OBEX P1 Docs P2 Docs Learn Events
Decimal to Integer conversion — Parallax Forums

Decimal to Integer conversion

g3cwig3cwi Posts: 262
edited 2012-01-26 12:56 in Propeller 1
Hi all

New to this but I want to set a loop to repeat a number of times based on a calculation. The calculation produces a decimal value and the repeat command appears to need an integer. I tried "Round" but that did not help. Any ideas?

Thanks

Richard

Comments

  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-01-26 12:07
    There's a technique known as "pseudoreal". You basicly use a multiplier such as 100 to do your computations and just keep in mind that your number is really one hundreth of the value in your variable.

    If you could be more specific about what you what to do we probably better help. What calculation produces which decimal value?
  • g3cwig3cwi Posts: 262
    edited 2012-01-26 12:13
    Hi Duane

    It is an audio beeper and I want to use the number of cycles to determine the length of the beep accurately:

    Cycles := ((1/Freq)*(Duration*1000)) 'Calculate the number of cycles for chosen duration

    repeat Cycles
    !outa[Pin] ' Toggle I/O pin
    waitcnt(ClkFreq/Freq + cnt) ' Delay here sets the frequency

    Cheers

    Richard
  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-01-26 12:34
    "1/Freq" will likely always be zero.

    Instead use:
    Cycles := (Duration*1000) / Freq 'Calculate the number of cycles for chosen duration
    
    

    You want to get in the habit of thinking of intermediate values.

    Follow this link to learn how to post code in the forum.

    attachment.php?attachmentid=78421&d=1297987572
  • g3cwig3cwi Posts: 262
    edited 2012-01-26 12:46
    Thanks Duane

    That's a useful tip!

    Regards

    Richard
  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-01-26 12:56
    Richard,

    Your welcome.

    By the way, welcome to the forum.

    Duane
Sign In or Register to comment.