[Pharo-dev] float printing glitch?

Stéphane Ducasse stephane.ducasse at inria.fr
Wed Dec 25 13:02:53 EST 2013


thanks nicolas.
I will give a try.

On 24 Dec 2013, at 18:15, Nicolas Cellier <nicolas.cellier.aka.nice at gmail.com> wrote:

> Yes, you should change the tickPrintBlock because it probably incorrectly use (aFloat roundTo: 0.1) printString.
> There is no guaranty that this return the nearest Float to the aimed decimal fraction.
> Since this is not the nearest Float, you have additional digits displayed.
> The print block should use a selector like printShowingDecimalPlaces: (Squeak) or something equivalent in Pharo (can't remember if the selectors are 100% compatible).
> 
> For completeness, do test these expressions:
> (0 to: 10) collect: [:i | (i/10) asFloat].
> versus
> (0 to: 10) collect: [:i | i*0.1].
> versus
> (0 to: 10) collect: [:i | (i/10) roundTo: 0.1].
> 
> Only the first one is correct. Trying to roundTo: 0.1 spoils thing because 0.1 is not exactly 1/10 and thus introduce small errors...
> 
> 
> 2013/12/24 Stéphane Ducasse <stephane.ducasse at inria.fr>
> Hi (nicolas :))
> 
> I was fixing the sound package and the EnvelopeEditorMorph shows a strange glitch
> 
> EnvelopeEditorMorph openOn: (FMSound brass1) copy title: 'brass1'
> 
> See ^^^^^^^^^^^^^^^^ below
> 
> I have the impression that this related to float printString. Any idea how to fix it?
> 
> 
> 
> 
> 
> buildLabels
>         | scale x1 y1 y2 captionMorph loopStart offset |
>         majorTickLength * minorTickLength < 0
>                 ifTrue: [ minorTickLength := 0 - minorTickLength ].
>         self removeAllMorphs.
>         caption ifNotNil:
>                 [ captionMorph := StringMorph contents: caption.
>                 offset := captionAbove
>                         ifTrue: [majorTickLength abs + captionMorph height + 7]
>                         ifFalse: [2].
>                 captionMorph
>                         align: captionMorph bounds bottomCenter
>                         with: self bounds bottomCenter - (0 @ offset).
>                 self addMorph: captionMorph].
>         tickPrintBlock ifNotNil:
>                         ["Calculate the offset for the labels, depending on whether or not
>                           1) there's a caption
>                         below, 2) the labels are above or below the ticks, and 3) the
>                         ticks go up or down"
>                         offset := labelsAbove
>                                         ifTrue: [majorTickLength abs + minorTickLength abs + 2]
>                                         ifFalse: [2].
>                         caption
>                                 ifNotNil: [captionAbove ifFalse: [offset := offset + captionMorph height + 2]].
>                         scale := (self innerBounds width - 1) / (stop - start) asFloat.
>                         x1 := self innerBounds left.
>                         y1 := self innerBounds bottom.
>                         y2 := y1 - offset.
>                         "Start loop on multiple of majorTick"
>                         loopStart := (start / majorTick) ceiling * majorTick.
>                         loopStart to: stop
>                                 by: majorTick
>                                 do:
>                                         [ :v | | x tickMorph |
>                                         x := x1 + (scale * (v - start)).
>                                         tickMorph := StringMorph contents: (tickPrintBlock value: v).
>                                                                                                  ^^^^^^^^^^^^^^^^ printString?
> 
>                                         tickMorph align: tickMorph bounds bottomCenter with: x @ y2.
>                                         tickMorph left < self left
>                                                 ifTrue: [ tickMorph position: self left @ tickMorph top ].
>                                         tickMorph right > self right
>                                                 ifTrue: [ tickMorph position: (self right - tickMorph width) @ tickMorph top].
>                                         self addMorph: tickMorph ]]
> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.pharo.org/pipermail/pharo-dev_lists.pharo.org/attachments/20131225/c11a483f/attachment-0002.html>


More information about the Pharo-dev mailing list