Csound Csound-dev Csound-tekno Search About

[Csnd] A puzzlement

Date2025-01-21 20:39
FromDave Seidel
Subject[Csnd] A puzzlement
In this code (adapted from a full piece), I am taking incoming MIDI notes, converting to pch (octave.pitch-class) form, and then using a macro to "transpose" the pitch-class portion so that it is three semitones lower, but I want to keep it in the 0-11 range. The transposed value is printed as "iidx".

The "transposition" is done with the Index macro, which as you can see subtracts 3, adds 13, and then returns the sum modulo 12.

When I play E-flat (MIDI 51), idx is 0 as expected. This is also true for any lower octave of E-flat. However, if I play E-flat one or more octaves higher (MIDI 63 or higher octaves), idx is 12.

I don't understand how this is possible. How can 12 % 12 ever be anything but 0?

I hope someone can explain this before I lose what is left of my mind! I'm using Csound 7 built from the latest development code as of yesterday.

- Dave

<CsoundSynthesizer>
<CsOptions>
-d -m128 -n -Ma
</CsOptions>

<CsInstruments>
sr=48000
ksmps=1
nchnls=2
0dbfs=1

#define Index(N)  #(($N - 3) + 12) % 12#

massign 0, 1
instr 1
    inote = notnum()
    ipch = pchmidinn(inote)
    ioct = int(ipch)
    itmp = frac(ipch) * 100
    print itmp
    iidx = $Index(itmp)
    prints("note=%d pch=%f oct=%f tmp=%f iidx=%f\n", inote, ipch, ioct, itmp, iidx)
endin
</CsInstruments>

<CsScore>
f 0 36000
e
</CsScore>
</CsoundSynthesizer>
Csound mailing list Csound@listserv.heanet.ie https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to https://github.com/csound/csound/issues Discussions of bugs and features can be posted here

Date2025-01-21 20:42
FromDave Seidel
SubjectRe: [Csnd] A puzzlement
Correction: the macro subtracts 3, then adds 12, and returns the sum modulo 12.

On Tue, Jan 21, 2025 at 3:39 PM Dave Seidel <dave.seidel@gmail.com> wrote:
In this code (adapted from a full piece), I am taking incoming MIDI notes, converting to pch (octave.pitch-class) form, and then using a macro to "transpose" the pitch-class portion so that it is three semitones lower, but I want to keep it in the 0-11 range. The transposed value is printed as "iidx".

The "transposition" is done with the Index macro, which as you can see subtracts 3, adds 13, and then returns the sum modulo 12.

When I play E-flat (MIDI 51), idx is 0 as expected. This is also true for any lower octave of E-flat. However, if I play E-flat one or more octaves higher (MIDI 63 or higher octaves), idx is 12.

I don't understand how this is possible. How can 12 % 12 ever be anything but 0?

I hope someone can explain this before I lose what is left of my mind! I'm using Csound 7 built from the latest development code as of yesterday.

- Dave

<CsoundSynthesizer>
<CsOptions>
-d -m128 -n -Ma
</CsOptions>

<CsInstruments>
sr=48000
ksmps=1
nchnls=2
0dbfs=1

#define Index(N)  #(($N - 3) + 12) % 12#

massign 0, 1
instr 1
    inote = notnum()
    ipch = pchmidinn(inote)
    ioct = int(ipch)
    itmp = frac(ipch) * 100
    print itmp
    iidx = $Index(itmp)
    prints("note=%d pch=%f oct=%f tmp=%f iidx=%f\n", inote, ipch, ioct, itmp, iidx)
endin
</CsInstruments>

<CsScore>
f 0 36000
e
</CsScore>
</CsoundSynthesizer>
Csound mailing list Csound@listserv.heanet.ie https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to https://github.com/csound/csound/issues Discussions of bugs and features can be posted here

Date2025-01-21 21:03
From"Jeanette C."
SubjectRe: [Csnd] A puzzlement
Hi Dave,
I see that you multiply the fractional pitch by 100. Shouldn't this be 10?

At any rate, why not, work on the MIDI note first and then convert? I don't 
know how much of a change it will make, but it seems simpler and thus less 
error prone and easier to debug.

Best wishes,

Jeanette

-- 
  * Website: http://juliencoder.de - for summer is a state of sound
  * Youtube: https://www.youtube.com/channel/UCMS4rfGrTwz8W7jhC1Jnv7g
  * Audiobombs: https://www.audiobombs.com/users/jeanette_c
  * GitHub: https://github.com/jeanette-c

There's a girl in the mirror
I wonder who she is
Sometimes I think I know her
Sometimes I really wish I did <3
(Britney Spears)

Csound mailing list
Csound@listserv.heanet.ie
https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND
Send bugs reports to
        https://github.com/csound/csound/issues
Discussions of bugs and features can be posted here

Date2025-01-21 21:10
FromVictor Lazzarini <000010b17ddd988e-dmarc-request@LISTSERV.HEANET.IE>
SubjectRe: [Csnd] [EXTERNAL] Re: [Csnd] A puzzlement
Without examining it in detail, I think the issue that you are encountering is that one
expects the frac(x)*100 to be a whole number but the floating point operations are not giving you that. 

What you are getting printed is an approximation so you don't see it immediately.

See for instance representable numbers in

Since all numbers in Csound are floating-point there are some surprising results in certain type of arithmetic expressions.

Prof. Victor Lazzarini
Maynooth University
Ireland

On 21 Jan 2025, at 20:42, Dave Seidel <dave.seidel@gmail.com> wrote:



*Warning*

This email originated from outside of Maynooth University's Mail System. Do not reply, click links or open attachments unless you recognise the sender and know the content is safe.

Correction: the macro subtracts 3, then adds 12, and returns the sum modulo 12.

On Tue, Jan 21, 2025 at 3:39 PM Dave Seidel <dave.seidel@gmail.com> wrote:
In this code (adapted from a full piece), I am taking incoming MIDI notes, converting to pch (octave.pitch-class) form, and then using a macro to "transpose" the pitch-class portion so that it is three semitones lower, but I want to keep it in the 0-11 range. The transposed value is printed as "iidx".

The "transposition" is done with the Index macro, which as you can see subtracts 3, adds 13, and then returns the sum modulo 12.

When I play E-flat (MIDI 51), idx is 0 as expected. This is also true for any lower octave of E-flat. However, if I play E-flat one or more octaves higher (MIDI 63 or higher octaves), idx is 12.

I don't understand how this is possible. How can 12 % 12 ever be anything but 0?

I hope someone can explain this before I lose what is left of my mind! I'm using Csound 7 built from the latest development code as of yesterday.

- Dave

<CsoundSynthesizer>
<CsOptions>
-d -m128 -n -Ma
</CsOptions>

<CsInstruments>
sr=48000
ksmps=1
nchnls=2
0dbfs=1

#define Index(N)  #(($N - 3) + 12) % 12#

massign 0, 1
instr 1
    inote = notnum()
    ipch = pchmidinn(inote)
    ioct = int(ipch)
    itmp = frac(ipch) * 100
    print itmp
    iidx = $Index(itmp)
    prints("note=%d pch=%f oct=%f tmp=%f iidx=%f\n", inote, ipch, ioct, itmp, iidx)
endin
</CsInstruments>

<CsScore>
f 0 36000
e
</CsScore>
</CsoundSynthesizer>
Csound mailing list Csound@listserv.heanet.ie https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to https://github.com/csound/csound/issues Discussions of bugs and features can be posted here

Date2025-01-21 21:18
FromDave Seidel
SubjectRe: [Csnd] A puzzlement
Thanks, Jeanette. 100 is correct, because the pitch-class number uses the two digits to the right of the decimal point. For example, the note C# would be (depending on the octave) something like 8.01, so to make .01 an integer it must be multiplied by 100.

That's an interesting idea of just working with the MIDI note directly, and I'll look into that. Thanks for the suggestion!

- Dave

On Tue, Jan 21, 2025 at 4:03 PM Jeanette C. <julien@mail.upb.de> wrote:
Hi Dave,
I see that you multiply the fractional pitch by 100. Shouldn't this be 10?

At any rate, why not, work on the MIDI note first and then convert? I don't
know how much of a change it will make, but it seems simpler and thus less
error prone and easier to debug.

Best wishes,

Jeanette

--
  * Website: http://juliencoder.de - for summer is a state of sound
  * Youtube: https://www.youtube.com/channel/UCMS4rfGrTwz8W7jhC1Jnv7g
  * Audiobombs: https://www.audiobombs.com/users/jeanette_c
  * GitHub: https://github.com/jeanette-c

There's a girl in the mirror
I wonder who she is
Sometimes I think I know her
Sometimes I really wish I did <3
(Britney Spears)

Csound mailing list
Csound@listserv.heanet.ie
https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND
Send bugs reports to
        https://github.com/csound/csound/issues
Discussions of bugs and features can be posted here
Csound mailing list Csound@listserv.heanet.ie https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to https://github.com/csound/csound/issues Discussions of bugs and features can be posted here

Date2025-01-21 21:21
FromDave Seidel
SubjectRe: [Csnd] [EXTERNAL] Re: [Csnd] A puzzlement
Thanks, Victor, I was afraid it would be something like that. Since there's no way to force Csound to treat a number as a plain (non floating point) integer, I may need to follow Jeanette's suggestion and work with the MIDI note number directly.

- Dave

On Tue, Jan 21, 2025 at 4:10 PM Victor Lazzarini <000010b17ddd988e-dmarc-request@listserv.heanet.ie> wrote:
Without examining it in detail, I think the issue that you are encountering is that one
expects the frac(x)*100 to be a whole number but the floating point operations are not giving you that. 

What you are getting printed is an approximation so you don't see it immediately.

See for instance representable numbers in

Since all numbers in Csound are floating-point there are some surprising results in certain type of arithmetic expressions.

Prof. Victor Lazzarini
Maynooth University
Ireland

On 21 Jan 2025, at 20:42, Dave Seidel <dave.seidel@gmail.com> wrote:



*Warning*

This email originated from outside of Maynooth University's Mail System. Do not reply, click links or open attachments unless you recognise the sender and know the content is safe.

Correction: the macro subtracts 3, then adds 12, and returns the sum modulo 12.

On Tue, Jan 21, 2025 at 3:39 PM Dave Seidel <dave.seidel@gmail.com> wrote:
In this code (adapted from a full piece), I am taking incoming MIDI notes, converting to pch (octave.pitch-class) form, and then using a macro to "transpose" the pitch-class portion so that it is three semitones lower, but I want to keep it in the 0-11 range. The transposed value is printed as "iidx".

The "transposition" is done with the Index macro, which as you can see subtracts 3, adds 13, and then returns the sum modulo 12.

When I play E-flat (MIDI 51), idx is 0 as expected. This is also true for any lower octave of E-flat. However, if I play E-flat one or more octaves higher (MIDI 63 or higher octaves), idx is 12.

I don't understand how this is possible. How can 12 % 12 ever be anything but 0?

I hope someone can explain this before I lose what is left of my mind! I'm using Csound 7 built from the latest development code as of yesterday.

- Dave

<CsoundSynthesizer>
<CsOptions>
-d -m128 -n -Ma
</CsOptions>

<CsInstruments>
sr=48000
ksmps=1
nchnls=2
0dbfs=1

#define Index(N)  #(($N - 3) + 12) % 12#

massign 0, 1
instr 1
    inote = notnum()
    ipch = pchmidinn(inote)
    ioct = int(ipch)
    itmp = frac(ipch) * 100
    print itmp
    iidx = $Index(itmp)
    prints("note=%d pch=%f oct=%f tmp=%f iidx=%f\n", inote, ipch, ioct, itmp, iidx)
endin
</CsInstruments>

<CsScore>
f 0 36000
e
</CsScore>
</CsoundSynthesizer>
Csound mailing list Csound@listserv.heanet.ie https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to https://github.com/csound/csound/issues Discussions of bugs and features can be posted here
Csound mailing list Csound@listserv.heanet.ie https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to https://github.com/csound/csound/issues Discussions of bugs and features can be posted here
Csound mailing list Csound@listserv.heanet.ie https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to https://github.com/csound/csound/issues Discussions of bugs and features can be posted here

Date2025-01-22 15:45
Fromjoachim heintz
SubjectRe: [Csnd] [EXTERNAL] Re: [Csnd] A puzzlement
hi dave -

you mention that "there's no way to force Csound to treat a number as a 
plain (non floating point) integer".

but when you use round(x)?

best -
	joachim


On 21/01/2025 22:21, Dave Seidel wrote:
> Thanks, Victor, I was afraid it would be something like that. Since 
> there's no way to force Csound to treat a number as a plain (non 
> floating point) integer, I may need to follow Jeanette's suggestion and 
> work with the MIDI note number directly.
> 
> - Dave
> 
> On Tue, Jan 21, 2025 at 4:10 PM Victor Lazzarini <000010b17ddd988e- 
> dmarc-request@listserv.heanet.ie  request@listserv.heanet.ie>> wrote:
> 
>     Without examining it in detail, I think the issue that you are
>     encountering is that one
>     expects the frac(x)*100 to be a whole number but the floating point
>     operations are not giving you that.
> 
>     What you are getting printed is an approximation so you don't see it
>     immediately.
> 
>     See for instance representable numbers in
>     https://en.m.wikipedia.org/wiki/Floating-point_arithmetic      en.m.wikipedia.org/wiki/Floating-point_arithmetic>
> 
>     Since all numbers in Csound are floating-point there are some
>     surprising results in certain type of arithmetic expressions.
> 
>     Prof. Victor Lazzarini
>     Maynooth University
>     Ireland
> 
>>     On 21 Jan 2025, at 20:42, Dave Seidel >     > wrote:
>>
>>     
>>
>>
>>       *Warning*
>>
>>     This email originated from outside of Maynooth University's Mail
>>     System. Do not reply, click links or open attachments unless you
>>     recognise the sender and know the content is safe.
>>
>>     Correction: the macro subtracts 3, then adds 12, and returns the
>>     sum modulo 12.
>>
>>     On Tue, Jan 21, 2025 at 3:39 PM Dave Seidel >     > wrote:
>>
>>         In this code (adapted from a full piece), I am taking incoming
>>         MIDI notes, converting to pch (octave.pitch-class) form, and
>>         then using a macro to "transpose" the pitch-class portion so
>>         that it is three semitones lower, but I want to keep it in the
>>         0-11 range. The transposed value is printed as "iidx".
>>
>>         The "transposition" is done with the Index macro, which as you
>>         can see subtracts 3, adds 13, and then returns the sum modulo 12.
>>
>>         When I play E-flat (MIDI 51), idx is 0 as expected. This is
>>         also true for any lower octave of E-flat. However, if I play
>>         E-flat one or more octaves higher (MIDI 63 or higher octaves),
>>         idx is 12.
>>
>>         I don't understand how this is possible. How can 12 % 12 ever
>>         be anything but 0?
>>
>>         I hope someone can explain this before I lose what is left of
>>         my mind! I'm using Csound 7 built from the latest development
>>         code as of yesterday.
>>
>>         - Dave
>>
>>         
>>         
>>         -d -m128 -n -Ma
>>         
>>
>>         
>>         sr=48000
>>         ksmps=1
>>         nchnls=2
>>         0dbfs=1
>>
>>         #define Index(N)  #(($N - 3) + 12) % 12#
>>
>>         massign 0, 1
>>         instr 1
>>             inote = notnum()
>>             ipch = pchmidinn(inote)
>>             ioct = int(ipch)
>>             itmp = frac(ipch) * 100
>>             print itmp
>>             iidx = $Index(itmp)
>>             prints("note=%d pch=%f oct=%f tmp=%f iidx=%f\n", inote,
>>         ipch, ioct, itmp, iidx)
>>         endin
>>         
>>
>>         
>>         f 0 36000
>>         e
>>         
>>         
>>
>>     Csound mailing list Csound@listserv.heanet.ie
>>      https://listserv.heanet.ie/cgi-
>>     bin/wa?A0=CSOUND 
>>     Send bugs reports to https://github.com/csound/csound/issues
>>      Discussions of bugs and
>>     features can be posted here
>     Csound mailing list Csound@listserv.heanet.ie
>      https://listserv.heanet.ie/cgi-
>     bin/wa?A0=CSOUND 
>     Send bugs reports to https://github.com/csound/csound/issues
>      Discussions of bugs and
>     features can be posted here 
> 
> Csound mailing list Csound@listserv.heanet.ie 
>  https://listserv.heanet.ie/cgi-bin/ 
> wa?A0=CSOUND  Send bugs 
> reports to https://github.com/csound/csound/issues  csound/csound/issues> Discussions of bugs and features can be posted here

Csound mailing list
Csound@listserv.heanet.ie
https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND
Send bugs reports to
        https://github.com/csound/csound/issues
Discussions of bugs and features can be posted here

Date2025-01-22 16:04
FromDave Seidel
SubjectRe: [Csnd] [EXTERNAL] Re: [Csnd] A puzzlement
I think that even if you use round or floor (or int), the underlying data representation is still a floating point number. Usually this is not an issue, I guess I just hit an edge case.

On Wed, Jan 22, 2025 at 10:45 AM joachim heintz <jh@joachimheintz.de> wrote:
hi dave -

you mention that "there's no way to force Csound to treat a number as a
plain (non floating point) integer".

but when you use round(x)?

best -
        joachim


On 21/01/2025 22:21, Dave Seidel wrote:
> Thanks, Victor, I was afraid it would be something like that. Since
> there's no way to force Csound to treat a number as a plain (non
> floating point) integer, I may need to follow Jeanette's suggestion and
> work with the MIDI note number directly.
>
> - Dave
>
> On Tue, Jan 21, 2025 at 4:10 PM Victor Lazzarini <000010b17ddd988e-
> dmarc-request@listserv.heanet.ie <mailto:000010b17ddd988e-dmarc-
> request@listserv.heanet.ie>> wrote:
>
>     Without examining it in detail, I think the issue that you are
>     encountering is that one
>     expects the frac(x)*100 to be a whole number but the floating point
>     operations are not giving you that.
>
>     What you are getting printed is an approximation so you don't see it
>     immediately.
>
>     See for instance representable numbers in
>     https://en.m.wikipedia.org/wiki/Floating-point_arithmetic <https://
>     en.m.wikipedia.org/wiki/Floating-point_arithmetic>
>
>     Since all numbers in Csound are floating-point there are some
>     surprising results in certain type of arithmetic expressions.
>
>     Prof. Victor Lazzarini
>     Maynooth University
>     Ireland
>
>>     On 21 Jan 2025, at 20:42, Dave Seidel <dave.seidel@gmail.com
>>     <mailto:dave.seidel@gmail.com>> wrote:
>>
>>     
>>
>>
>>       *Warning*
>>
>>     This email originated from outside of Maynooth University's Mail
>>     System. Do not reply, click links or open attachments unless you
>>     recognise the sender and know the content is safe.
>>
>>     Correction: the macro subtracts 3, then adds 12, and returns the
>>     sum modulo 12.
>>
>>     On Tue, Jan 21, 2025 at 3:39 PM Dave Seidel <dave.seidel@gmail.com
>>     <mailto:dave.seidel@gmail.com>> wrote:
>>
>>         In this code (adapted from a full piece), I am taking incoming
>>         MIDI notes, converting to pch (octave.pitch-class) form, and
>>         then using a macro to "transpose" the pitch-class portion so
>>         that it is three semitones lower, but I want to keep it in the
>>         0-11 range. The transposed value is printed as "iidx".
>>
>>         The "transposition" is done with the Index macro, which as you
>>         can see subtracts 3, adds 13, and then returns the sum modulo 12.
>>
>>         When I play E-flat (MIDI 51), idx is 0 as expected. This is
>>         also true for any lower octave of E-flat. However, if I play
>>         E-flat one or more octaves higher (MIDI 63 or higher octaves),
>>         idx is 12.
>>
>>         I don't understand how this is possible. How can 12 % 12 ever
>>         be anything but 0?
>>
>>         I hope someone can explain this before I lose what is left of
>>         my mind! I'm using Csound 7 built from the latest development
>>         code as of yesterday.
>>
>>         - Dave
>>
>>         <CsoundSynthesizer>
>>         <CsOptions>
>>         -d -m128 -n -Ma
>>         </CsOptions>
>>
>>         <CsInstruments>
>>         sr=48000
>>         ksmps=1
>>         nchnls=2
>>         0dbfs=1
>>
>>         #define Index(N)  #(($N - 3) + 12) % 12#
>>
>>         massign 0, 1
>>         instr 1
>>             inote = notnum()
>>             ipch = pchmidinn(inote)
>>             ioct = int(ipch)
>>             itmp = frac(ipch) * 100
>>             print itmp
>>             iidx = $Index(itmp)
>>             prints("note=%d pch=%f oct=%f tmp=%f iidx=%f\n", inote,
>>         ipch, ioct, itmp, iidx)
>>         endin
>>         </CsInstruments>
>>
>>         <CsScore>
>>         f 0 36000
>>         e
>>         </CsScore>
>>         </CsoundSynthesizer>
>>
>>     Csound mailing list Csound@listserv.heanet.ie
>>     <mailto:Csound@listserv.heanet.ie> https://listserv.heanet.ie/cgi-
>>     bin/wa?A0=CSOUND <https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND>
>>     Send bugs reports to https://github.com/csound/csound/issues
>>     <https://github.com/csound/csound/issues> Discussions of bugs and
>>     features can be posted here
>     Csound mailing list Csound@listserv.heanet.ie
>     <mailto:Csound@listserv.heanet.ie> https://listserv.heanet.ie/cgi-
>     bin/wa?A0=CSOUND <https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND>
>     Send bugs reports to https://github.com/csound/csound/issues
>     <https://github.com/csound/csound/issues> Discussions of bugs and
>     features can be posted here
>
> Csound mailing list Csound@listserv.heanet.ie
> <mailto:Csound@listserv.heanet.ie> https://listserv.heanet.ie/cgi-bin/
> wa?A0=CSOUND <https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND> Send bugs
> reports to https://github.com/csound/csound/issues <https://github.com/
> csound/csound/issues> Discussions of bugs and features can be posted here

Csound mailing list
Csound@listserv.heanet.ie
https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND
Send bugs reports to
        https://github.com/csound/csound/issues
Discussions of bugs and features can be posted here
Csound mailing list Csound@listserv.heanet.ie https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to https://github.com/csound/csound/issues Discussions of bugs and features can be posted here

Date2025-01-22 16:16
FromSteven Yi
SubjectRe: [Csnd] [EXTERNAL] Re: [Csnd] A puzzlement
This seems to work here:

#define Index(N)  #round(($N - 3) + 12) % 12#

On Wed, Jan 22, 2025 at 11:04 AM Dave Seidel <dave.seidel@gmail.com> wrote:
I think that even if you use round or floor (or int), the underlying data representation is still a floating point number. Usually this is not an issue, I guess I just hit an edge case.

On Wed, Jan 22, 2025 at 10:45 AM joachim heintz <jh@joachimheintz.de> wrote:
hi dave -

you mention that "there's no way to force Csound to treat a number as a
plain (non floating point) integer".

but when you use round(x)?

best -
        joachim


On 21/01/2025 22:21, Dave Seidel wrote:
> Thanks, Victor, I was afraid it would be something like that. Since
> there's no way to force Csound to treat a number as a plain (non
> floating point) integer, I may need to follow Jeanette's suggestion and
> work with the MIDI note number directly.
>
> - Dave
>
> On Tue, Jan 21, 2025 at 4:10 PM Victor Lazzarini <000010b17ddd988e-
> dmarc-request@listserv.heanet.ie <mailto:000010b17ddd988e-dmarc-
> request@listserv.heanet.ie>> wrote:
>
>     Without examining it in detail, I think the issue that you are
>     encountering is that one
>     expects the frac(x)*100 to be a whole number but the floating point
>     operations are not giving you that.
>
>     What you are getting printed is an approximation so you don't see it
>     immediately.
>
>     See for instance representable numbers in
>     https://en.m.wikipedia.org/wiki/Floating-point_arithmetic <https://
>     en.m.wikipedia.org/wiki/Floating-point_arithmetic>
>
>     Since all numbers in Csound are floating-point there are some
>     surprising results in certain type of arithmetic expressions.
>
>     Prof. Victor Lazzarini
>     Maynooth University
>     Ireland
>
>>     On 21 Jan 2025, at 20:42, Dave Seidel <dave.seidel@gmail.com
>>     <mailto:dave.seidel@gmail.com>> wrote:
>>
>>     
>>
>>
>>       *Warning*
>>
>>     This email originated from outside of Maynooth University's Mail
>>     System. Do not reply, click links or open attachments unless you
>>     recognise the sender and know the content is safe.
>>
>>     Correction: the macro subtracts 3, then adds 12, and returns the
>>     sum modulo 12.
>>
>>     On Tue, Jan 21, 2025 at 3:39 PM Dave Seidel <dave.seidel@gmail.com
>>     <mailto:dave.seidel@gmail.com>> wrote:
>>
>>         In this code (adapted from a full piece), I am taking incoming
>>         MIDI notes, converting to pch (octave.pitch-class) form, and
>>         then using a macro to "transpose" the pitch-class portion so
>>         that it is three semitones lower, but I want to keep it in the
>>         0-11 range. The transposed value is printed as "iidx".
>>
>>         The "transposition" is done with the Index macro, which as you
>>         can see subtracts 3, adds 13, and then returns the sum modulo 12.
>>
>>         When I play E-flat (MIDI 51), idx is 0 as expected. This is
>>         also true for any lower octave of E-flat. However, if I play
>>         E-flat one or more octaves higher (MIDI 63 or higher octaves),
>>         idx is 12.
>>
>>         I don't understand how this is possible. How can 12 % 12 ever
>>         be anything but 0?
>>
>>         I hope someone can explain this before I lose what is left of
>>         my mind! I'm using Csound 7 built from the latest development
>>         code as of yesterday.
>>
>>         - Dave
>>
>>         <CsoundSynthesizer>
>>         <CsOptions>
>>         -d -m128 -n -Ma
>>         </CsOptions>
>>
>>         <CsInstruments>
>>         sr=48000
>>         ksmps=1
>>         nchnls=2
>>         0dbfs=1
>>
>>         #define Index(N)  #(($N - 3) + 12) % 12#
>>
>>         massign 0, 1
>>         instr 1
>>             inote = notnum()
>>             ipch = pchmidinn(inote)
>>             ioct = int(ipch)
>>             itmp = frac(ipch) * 100
>>             print itmp
>>             iidx = $Index(itmp)
>>             prints("note=%d pch=%f oct=%f tmp=%f iidx=%f\n", inote,
>>         ipch, ioct, itmp, iidx)
>>         endin
>>         </CsInstruments>
>>
>>         <CsScore>
>>         f 0 36000
>>         e
>>         </CsScore>
>>         </CsoundSynthesizer>
>>
>>     Csound mailing list Csound@listserv.heanet.ie
>>     <mailto:Csound@listserv.heanet.ie> https://listserv.heanet.ie/cgi-
>>     bin/wa?A0=CSOUND <https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND>
>>     Send bugs reports to https://github.com/csound/csound/issues
>>     <https://github.com/csound/csound/issues> Discussions of bugs and
>>     features can be posted here
>     Csound mailing list Csound@listserv.heanet.ie
>     <mailto:Csound@listserv.heanet.ie> https://listserv.heanet.ie/cgi-
>     bin/wa?A0=CSOUND <https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND>
>     Send bugs reports to https://github.com/csound/csound/issues
>     <https://github.com/csound/csound/issues> Discussions of bugs and
>     features can be posted here
>
> Csound mailing list Csound@listserv.heanet.ie
> <mailto:Csound@listserv.heanet.ie> https://listserv.heanet.ie/cgi-bin/
> wa?A0=CSOUND <https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND> Send bugs
> reports to https://github.com/csound/csound/issues <https://github.com/
> csound/csound/issues> Discussions of bugs and features can be posted here

Csound mailing list
Csound@listserv.heanet.ie
https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND
Send bugs reports to
        https://github.com/csound/csound/issues
Discussions of bugs and features can be posted here
Csound mailing list Csound@listserv.heanet.ie https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to https://github.com/csound/csound/issues Discussions of bugs and features can be posted here
Csound mailing list Csound@listserv.heanet.ie https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to https://github.com/csound/csound/issues Discussions of bugs and features can be posted here

Date2025-01-22 16:38
FromDave Seidel
SubjectRe: [Csnd] [EXTERNAL] Re: [Csnd] A puzzlement
It does work, in general. If I put that expression in a loop where N runs from 0 to 11, all the answers are correct. But in context, where N is derived from a pch value and promoted to an integer using "frac(ipch) * 100", it fails for inputs over a certain value. This is 100% repeatable, at least for me.

I have switched to just using "(inote - 3) % 12", where inote is the value returned from notnum(), and this is working for all input values.

On Wed, Jan 22, 2025 at 11:17 AM Steven Yi <stevenyi@gmail.com> wrote:
This seems to work here:

#define Index(N)  #round(($N - 3) + 12) % 12#

On Wed, Jan 22, 2025 at 11:04 AM Dave Seidel <dave.seidel@gmail.com> wrote:
I think that even if you use round or floor (or int), the underlying data representation is still a floating point number. Usually this is not an issue, I guess I just hit an edge case.

On Wed, Jan 22, 2025 at 10:45 AM joachim heintz <jh@joachimheintz.de> wrote:
hi dave -

you mention that "there's no way to force Csound to treat a number as a
plain (non floating point) integer".

but when you use round(x)?

best -
        joachim


On 21/01/2025 22:21, Dave Seidel wrote:
> Thanks, Victor, I was afraid it would be something like that. Since
> there's no way to force Csound to treat a number as a plain (non
> floating point) integer, I may need to follow Jeanette's suggestion and
> work with the MIDI note number directly.
>
> - Dave
>
> On Tue, Jan 21, 2025 at 4:10 PM Victor Lazzarini <000010b17ddd988e-
> dmarc-request@listserv.heanet.ie <mailto:000010b17ddd988e-dmarc-
> request@listserv.heanet.ie>> wrote:
>
>     Without examining it in detail, I think the issue that you are
>     encountering is that one
>     expects the frac(x)*100 to be a whole number but the floating point
>     operations are not giving you that.
>
>     What you are getting printed is an approximation so you don't see it
>     immediately.
>
>     See for instance representable numbers in
>     https://en.m.wikipedia.org/wiki/Floating-point_arithmetic <https://
>     en.m.wikipedia.org/wiki/Floating-point_arithmetic>
>
>     Since all numbers in Csound are floating-point there are some
>     surprising results in certain type of arithmetic expressions.
>
>     Prof. Victor Lazzarini
>     Maynooth University
>     Ireland
>
>>     On 21 Jan 2025, at 20:42, Dave Seidel <dave.seidel@gmail.com
>>     <mailto:dave.seidel@gmail.com>> wrote:
>>
>>     
>>
>>
>>       *Warning*
>>
>>     This email originated from outside of Maynooth University's Mail
>>     System. Do not reply, click links or open attachments unless you
>>     recognise the sender and know the content is safe.
>>
>>     Correction: the macro subtracts 3, then adds 12, and returns the
>>     sum modulo 12.
>>
>>     On Tue, Jan 21, 2025 at 3:39 PM Dave Seidel <dave.seidel@gmail.com
>>     <mailto:dave.seidel@gmail.com>> wrote:
>>
>>         In this code (adapted from a full piece), I am taking incoming
>>         MIDI notes, converting to pch (octave.pitch-class) form, and
>>         then using a macro to "transpose" the pitch-class portion so
>>         that it is three semitones lower, but I want to keep it in the
>>         0-11 range. The transposed value is printed as "iidx".
>>
>>         The "transposition" is done with the Index macro, which as you
>>         can see subtracts 3, adds 13, and then returns the sum modulo 12.
>>
>>         When I play E-flat (MIDI 51), idx is 0 as expected. This is
>>         also true for any lower octave of E-flat. However, if I play
>>         E-flat one or more octaves higher (MIDI 63 or higher octaves),
>>         idx is 12.
>>
>>         I don't understand how this is possible. How can 12 % 12 ever
>>         be anything but 0?
>>
>>         I hope someone can explain this before I lose what is left of
>>         my mind! I'm using Csound 7 built from the latest development
>>         code as of yesterday.
>>
>>         - Dave
>>
>>         <CsoundSynthesizer>
>>         <CsOptions>
>>         -d -m128 -n -Ma
>>         </CsOptions>
>>
>>         <CsInstruments>
>>         sr=48000
>>         ksmps=1
>>         nchnls=2
>>         0dbfs=1
>>
>>         #define Index(N)  #(($N - 3) + 12) % 12#
>>
>>         massign 0, 1
>>         instr 1
>>             inote = notnum()
>>             ipch = pchmidinn(inote)
>>             ioct = int(ipch)
>>             itmp = frac(ipch) * 100
>>             print itmp
>>             iidx = $Index(itmp)
>>             prints("note=%d pch=%f oct=%f tmp=%f iidx=%f\n", inote,
>>         ipch, ioct, itmp, iidx)
>>         endin
>>         </CsInstruments>
>>
>>         <CsScore>
>>         f 0 36000
>>         e
>>         </CsScore>
>>         </CsoundSynthesizer>
>>
>>     Csound mailing list Csound@listserv.heanet.ie
>>     <mailto:Csound@listserv.heanet.ie> https://listserv.heanet.ie/cgi-
>>     bin/wa?A0=CSOUND <https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND>
>>     Send bugs reports to https://github.com/csound/csound/issues
>>     <https://github.com/csound/csound/issues> Discussions of bugs and
>>     features can be posted here
>     Csound mailing list Csound@listserv.heanet.ie
>     <mailto:Csound@listserv.heanet.ie> https://listserv.heanet.ie/cgi-
>     bin/wa?A0=CSOUND <https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND>
>     Send bugs reports to https://github.com/csound/csound/issues
>     <https://github.com/csound/csound/issues> Discussions of bugs and
>     features can be posted here
>
> Csound mailing list Csound@listserv.heanet.ie
> <mailto:Csound@listserv.heanet.ie> https://listserv.heanet.ie/cgi-bin/
> wa?A0=CSOUND <https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND> Send bugs
> reports to https://github.com/csound/csound/issues <https://github.com/
> csound/csound/issues> Discussions of bugs and features can be posted here

Csound mailing list
Csound@listserv.heanet.ie
https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND
Send bugs reports to
        https://github.com/csound/csound/issues
Discussions of bugs and features can be posted here
Csound mailing list Csound@listserv.heanet.ie https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to https://github.com/csound/csound/issues Discussions of bugs and features can be posted here
Csound mailing list Csound@listserv.heanet.ie https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to https://github.com/csound/csound/issues Discussions of bugs and features can be posted here
Csound mailing list Csound@listserv.heanet.ie https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to https://github.com/csound/csound/issues Discussions of bugs and features can be posted here