[C++] Fractal en integer part

Pagina: 1
Acties:

  • woutertjez
  • Registratie: Januari 2001
  • Laatst online: 02-09 10:02
Is het in C++ mogelijk om van een floatingpoint getal het gedeelte voor de komma en achter de komma te scheiden?

Bijvoorbeeld je hebt het getal 2.42345543213

Door het toepassen van een commando (oid) zou je dan bijvoorbeeld kunnen zeggen:
i = fract(2.42345543213)
Waardoor i dus de waarde 0.42345543213 krijgt.

Is zoiets mogelijk en zo ja, hoe?

  • whoami
  • Registratie: December 2000
  • Laatst online: 21:42
ja.
code:
1
2
int geheel = (int)getal;
float fractal = getal - geheel;

https://fgheysels.github.io/


  • woutertjez
  • Registratie: Januari 2001
  • Laatst online: 02-09 10:02
Thx :), maar is er geen commando om dat direct te doen?

  • victorv
  • Registratie: Januari 2002
  • Laatst online: 17-02-2024

victorv

Locallost

code:
1
2
int geheel = (int)getal;
float fractal = getal - geheel;
Dat dacht ik ook maar door de int casting kan een afrondingsverschil ontstaan. Ik had een double 50.90 vermenigvuldigd met 100.0 en gecast naar een long (long lPrice=(long)dPrice*100.0). De long gaf 5089 aan!? Ik ben uiteindelijk op het volgende uitgekomen:
code:
1
2
3
4
5
6
#include <math.h>

double dPrice = 50.90;
double dIntegeredPrice;
modf(dPrice * 100.0, &dIntegeredPrice);
long lPrice = (long)dIntegeredPrice;

"Accomplishing the impossible means only that the boss will add it to your regular duties."


  • whoami
  • Registratie: December 2000
  • Laatst online: 21:42
Op woensdag 24 april 2002 11:09 schreef woutertjez het volgende:
Thx :), maar is er geen commando om dat direct te doen?
Niet naar ik weet. Je kunt natuurlijk zelf een functie schrijven die gewoon die code uitvoert en die functie overloaden of in een template gieten.

https://fgheysels.github.io/


  • woutertjez
  • Registratie: Januari 2001
  • Laatst online: 02-09 10:02
Heb ff een boek gekregen van een collega:

In math.h staat er een functie om het integer gedeelte te verkrijgen:
floor(x) Gives the greatest integer which is <= x
ceil(x) Gives the smallest integer which is >= x

Dus dan zou ik kunnen schrijven:
code:
1
2
3
4
#include <math.h>

x = 2.42345543213
i = x - floor(x)

  • curry684
  • Registratie: Juni 2000
  • Laatst online: 04-09 14:38

curry684

left part of the evil twins

modf
Splits a floating-point value into fractional and integer parts.
code:
1
double modf( double x, double *intptr );

Routine modf
Required Header <math.h>
Compatibility ANSI, Win 95, Win NT

Return Value

This function returns the signed fractional portion of x. There is no error return.

Parameters

x Floating-point value

intptr Pointer to stored integer portion

Remarks

The modf function breaks down the floating-point value x into fractional and integer parts, each of which has the same sign as x. The signed fractional portion of x is returned. The integer portion is stored as a floating-point value at intptr.

Example:
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/* MODF.C */

#include <math.h>
#include <stdio.h>

void main( void )
{
   double x, y, n;

   x = -14.87654321;    /* Divide x into its fractional */
   y = modf( x, &n );     /* and integer parts      */

   printf( "For %f, the fraction is %f and the integer is %.f\n", 
         x, y, n );
}

Output
For -14.876543, the fraction is -0.876543 and the integer is -14

Professionele website nodig?


  • woutertjez
  • Registratie: Januari 2001
  • Laatst online: 02-09 10:02
:)
Thx, modf wordt niet genoemd in dat boek, alleen fmod (wat de rest na deling teruggeeft).

  • MSalters
  • Registratie: Juni 2001
  • Laatst online: 09-09 00:49
Op woensdag 24 april 2002 11:10 schreef Sonii het volgende:

[..]

Dat dacht ik ook maar door de int casting kan een afrondingsverschil ontstaan. Ik had een double 50.90 vermenigvuldigd met 100.0 en gecast naar een long (long lPrice=(long)dPrice*100.0). De long gaf 5089 aan!?
Er is geen float gelijk aan 50.90 De compiler moet dus afronden op 50.89999nogwat, dus vermenivuldigd met 100.0 wordt dat 5089.999nogwat en dat rond dus af op 5089.

Computers zijn nl. binair, en .90 heeft een oneindige
binaire expansie (net zoals 1/3 in decimaal)

Man hopes. Genius creates. Ralph Waldo Emerson
Never worry about theory as long as the machinery does what it's supposed to do. R. A. Heinlein

Pagina: 1